因为内部内的构造器必须连接到指向外围类对象的引用,所以在继承内部类的时候,事情会变得有点复杂。问题在于,那个指向外国类对象的“秘密的”引用必须被初始化,而在导出类中不再存在可连接的默认对象。要解决这个问题,必须使用特殊的语法来明确说清它们之间的关联:(摘抄自Thinking In Java Fourth Edition)
package edu.dlut.zxf.test;
class OutClass {
public class InnerClass {
public InnerClass() { }
}
}
class InheritInnerClass extends OutClass.InnerClass {
//public InheritInnerClass() { } //不能通过编译
public InheritInnerClass(OutClass out) {
out.super();
}
}
public class Test {
public static void main(String[] args) {
OutClass out = new OutClass();
InheritInnerClass inherit = new InheritInnerClass(out);
System.out.println(inherit);
}
}
|
再看一例:
package edu.dlut.zxf.test;
class OutClass {
public class InnerClass {
private String s;
public InnerClass(String s) {
this.s = s;
}
public String toString() {
return s;
}
}
}
class InheritInnerClass extends OutClass.InnerClass {
//public InheritInnerClass() { } //不能通过编译
public InheritInnerClass(OutClass out) {
out.super("hello, world");
System.out.println(this);
}
}
public class Test {
public static void main(String[] args) {
OutClass out = new OutClass();
InheritInnerClass inherit = new InheritInnerClass(out);
System.out.println(inherit);
}
}
|
阅读(1081) | 评论(0) | 转发(0) |