java内部类对象使用.this,.new

public class InnerClass {

    class Content {
        private int i;

        public int getVlaue() {
            return i;
        }

    }


    class Description {

        private String lable;

        Description(String lab) {
            this.lable = lab;

        }

        public String readLable() {
            return lable;
        }
    }

    public Content getContentInstance() {

        return new Content();
    }

    public Description getDescriptionIntance(String lable) {

        return new Description(lable);
    }

    public void ship(String lable) {

        Content content = getContentInstance();

        Description description = getDescriptionIntance(lable);

        System.out.println(description.readLable());


        System.out.println(description.readLable());


    }

    /**
     * 在创建外部类对象前,是不可以创建内部类对象的,因为内部类对象会暗暗的连接到外部类对象之上。<p>
     * 如果你想通过外围类对象创建内部类对象  之前已经说过最简单的方法是在外围类声明一个方法指向其内部类的对象。另一种更加简单的做法
     * JAVA支持通过外围类对象.new 语法表达一个外围类对象的引用
     * @param args
     */
    public static void main(String[] args) {

        InnerClass parcle2 = new InnerClass();

        InnerClass.Content c = parcle2.new Content();
        System.out.println(c.getVlaue());
        // parcle2.ship("hi");
        // InnerClass.Content c = parcle2.getContentInstance();// 如果想在外部类的非静态方法之外的任意位置访问某个内部类的对象,那么必须通过OutClass.xx

        InnerClass.Description d = parcle2.new Description("hello");
        System.out.println(d.readLable());
        // InnerClass.Description d = parcle2.getDescriptionIntance("hello");
    }


}
View Code

使用.this,.new

.this  表达的是在内部类对象域内  通过外部类.this 指向了一个在内部类指向外围类对象引用的关系。只有这样可以访问外围类对象的属性与方法

.new表达的是与.this方向相反  当在外围类作用域上 想创建内部类对象  之前通用的做法是 在外围类创建一个指向内部类的引用来创建内部类,但有一种更加快捷的方式

直接外围类.new  就可以表达一个外围类对象引用 。这里必须强调一点  在拥有外部类对象之前是不可能创建外围类对象的,因为内部类对象会暗暗的连接到创建他的外围类对象上

改变一下上面的内部类例子

public class InnerClass {

    class Content {
        private int i;

        public int getVlaue() {
            return i;
        }

    }


    class Description {

        private String lable;

        Description(String lab) {
            this.lable = lab;

        }

        public String readLable() {
            return lable;
        }
    }

    public Content getContentInstance() {

        return new Content();
    }

    public Description getDescriptionIntance(String lable) {

        return new Description(lable);
    }

    public void ship(String lable) {

        Content content = getContentInstance();

        Description description = getDescriptionIntance(lable);

        System.out.println(description.readLable());


        System.out.println(description.readLable());


    }

    /**
     * 在创建外部类对象前,是不可以创建内部类对象的,因为内部类对象会暗暗的连接到外部类对象之上。<p>
     * 如果你想通过外围类对象创建内部类对象  之前已经说过最简单的方法是在外围类声明一个方法指向其内部类的对象。另一种更加简单的做法
     * JAVA支持通过外围类对象.new 语法表达一个外围类对象的引用
     * @param args
     */
    public static void main(String[] args) {

        InnerClass parcle2 = new InnerClass();

        InnerClass.Content c = parcle2.new Content();
        System.out.println(c.getVlaue());
        // parcle2.ship("hi");
        // InnerClass.Content c = parcle2.getContentInstance();// 如果想在外部类的非静态方法之外的任意位置访问某个内部类的对象,那么必须通过OutClass.xx

        InnerClass.Description d = parcle2.new Description("hello");
        System.out.println(d.readLable());
        // InnerClass.Description d = parcle2.getDescriptionIntance("hello");
    }


}
View Code

猜你喜欢

转载自www.cnblogs.com/zhangfengshi/p/9386036.html
今日推荐