美文网首页
匿名对象

匿名对象

作者: dab7927433f9 | 来源:发表于2018-11-06 23:40 被阅读17次

    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";

    }

    }

    匿名对象

    相关文章

      网友评论

          本文标题:匿名对象

          本文链接:https://www.haomeiwen.com/subject/maapxqtx.html