Java学习笔记之内部类

关于new和this:

If you need to produce the reference to the outer-class object, you name the outer class followed by a dot and this.

当内部类需要指向外部类的引用的时候,可以直接用外部类名.this

public class DotThis {
    void f() { System.out.println("DotThis.f()"); }
    public class Inner {
    public DotThis outer() {
    return DotThis.this;
    // A plain "this" would be Inner’s "this"
    }
    }
    public Inner inner() { return new Inner(); }
    public static void main(String[] args) {
    DotThis dt = new DotThis();
    DotThis.Inner dti = dt.inner();
    dti.outer().f();
    }
    }


输出:DotThis.f()

如果需要创建某个类的内部类对象,用外部类对象.new  内部类的构造函数

DotThis dn = new DotThis();

    DotThis.Inner dni = dn.new Inner();


2匿名类:(转载)

匿名内部类。
顾名思义,没有名字的内部类。表面上看起来它们似乎有名字,实际那不是它们的名字。

A、继承式的匿名内部类。
    class Car {
        public void drive(){
            System.out.println("Driving a car!");
        }
    }
    
    class Test{
        public static void main(String[] args) {
            Car car = new Car(){
                public void drive(){
                    System.out.println("Driving another car!");
                }
            };
            car.drive();
        }
    }
结果输出了:Driving another car! Car引用变量不是引用Car对象,而是Car匿名子类的对象。
建立匿名内部类的关键点是重写父类的一个或多个方法。再强调一下,是重写父类的方法,而不是创建新的方法。因为用父类的引用不可能调用父类本身没有的方法!创建新的方法是多余的。简言之,参考多态。

B、接口式的匿名内部类。
    interface  Vehicle {
        public void drive();
    }
    
    class Test{
        public static void main(String[] args) {
            Vehicle v = new Vehicle(){
                public void drive(){
                    System.out.println("Driving a car!");
                }
            };
            v.drive();
        }
    }
上面的代码很怪,好像是在实例化一个接口。事实并非如此,接口式的匿名内部类是实现了一个接口的匿名类。而且只能实现一个接口。

C、参数式的匿名内部类。
class Bar{
    void doStuff(Foo f){}
}

interface Foo{
    void foo();
}

class Test{
    static void go(){
        Bar b = new Bar();
        b.doStuff(new Foo(){
            public void foo(){
                System.out.println("foofy");
            }
        });
    }
}


Each inner class can independently inherit from an implementation. Thus, the inner class is not limited by whether the outer class is already inheriting from an implementation.


内部类的继承:先上代码,再慢慢理解。

//: innerclasses/InheritInner.java
// Inheriting an inner class.
class WithInner {
class Inner {}
}
public class InheritInner extends WithInner.Inner {
//! InheritInner() {} // Won’t compile
InheritInner(WithInner wi) {
wi.super();
}
public static void main(String[] args) {
WithInner wi = new WithInner();
InheritInner ii = new InheritInner(wi);
}
} ///:~


But when it comes time to create a constructor, the default one is no good, and you can’t just pass a reference to an enclosing object. In addition, you must use the syntax
enclosingClassReference.super();
inside the constructor.



This example shows that there isn’t any extra inner-class magic going on when you inherit from the outer class. The two inner classes are completely separate entities, each in its own namespace. However, it’s still possible to explicitly inherit from the inner class:
//: innerclasses/BigEgg2.java
// Proper inheritance of an inner class.
import static net.mindview.util.Print.*;
class Egg2 {
protected class Yolk {
public Yolk() { print("Egg2.Yolk()"); }
public void f() { print("Egg2.Yolk.f()");}
}
private Yolk y = new Yolk();
public Egg2() { print("New Egg2()"); }
public void insertYolk(Yolk yy) { y = yy; }
public void g() { y.f(); }
}
public class BigEgg2 extends Egg2 {
public class Yolk extends Egg2.Yolk {
public Yolk() { print("BigEgg2.Yolk()"); }
public void f() { print("BigEgg2.Yolk.f()"); }
}
public BigEgg2() { insertYolk(new Yolk()); }
public static void main(String[] args) {
Egg2 e2 = new BigEgg2();
e2.g();
}
} /* Output:
Egg2.Yolk()
New Egg2()
Egg2.Yolk()
BigEgg2.Yolk()
BigEgg2.Yolk.f()
*///:~

Now BigEgg2.Yolk explicitly extends Egg2.Yolk and overrides its methods. The method insertYolk( ) allows BigEgg2 to upcast one of its own Yolk objects into the y reference in Egg2, so when g( ) calls y.f( ), the overridden version of f( ) is used. The second call to Egg2.Yolk( ) is the base-class constructor call of the BigEgg2.Yolk constructor. You can see that the overridden version of f( ) is used when g( ) is called.


你可能感兴趣的:(java,String,Class,reference,inheritance,Constructor)