匿名对象

2018-11-06  本文已影响17人  dab7927433f9

1.匿名对象就是没有名字的对象,如果程序中只是用一次该对象,就可以使用匿名对象的方式

引用传递

1.范例

class Ref{

int temp=10;

}

public class F02 {

  public static void main(String args[]) {

  Ref r1=new Ref();

  r1.temp=20;

  System.out.println(r1.temp);

  tell(r1);

  System.out.println(r1.temp);

  }

  public static void tell(Ref r2) {

  r2.temp=30;

  }

}

匿名对象

public class F03 {

public static void main(String[] args) {

String  str1="hello";

System.out.println(str1);

tell(str1);

System.out.println(str1);

}

    public static void tell(String str2) {

    str2="word";

    }

}

匿名对象

String数值类型不可改变的原因。

class Ref2 {

String temp="hello";

}

public class F04 {

public static void main(String[] args) {

Ref2 r1= new Ref2();

r1.temp="word";

System.out.println(r1.temp);

        tell(r1);

        System.out.println(r1);

}

public static void tell(Ref2 r2) {

r2.temp ="happy";

}

}

匿名对象

上一篇下一篇

猜你喜欢

热点阅读