abstract class Contents {
abstract public int value();
}
//接口 Destintion
interface Destination {
String readLabel();
} class Parcel3 {
//私有的内类继承抽象类contents
private class PContents extends Contents {
private int i = 11;
public int value() { return i; }
}
//内类实现接口
protected class PDestination
implements Destination {
private String label;
//注意这个构造器是私有的???????????????????????????????????
private PDestination(String whereTo) {
label = whereTo;
}
public String readLabel() { return label; }
}
//问题就是这个方法,他调用了PDestination(s)私有的构造器
public Destination dest(String s) {
return new PDestination(s); //构造器是私有的怎么可以new?
}
public Contents cont() {
return new PContents();
}
} class Test {
public static void main(String[] args) {
Parcel3 p = new Parcel3();
Contents c = p.cont();
Destination d = p.dest("Tanzania");
// Illegal -- can't access private class:
//! Parcel3.PContents c = p.new PContents();
}
}
为什么可以调用私有的构造器?
abstract public int value();
}
//接口 Destintion
interface Destination {
String readLabel();
} class Parcel3 {
//私有的内类继承抽象类contents
private class PContents extends Contents {
private int i = 11;
public int value() { return i; }
}
//内类实现接口
protected class PDestination
implements Destination {
private String label;
//注意这个构造器是私有的???????????????????????????????????
private PDestination(String whereTo) {
label = whereTo;
}
public String readLabel() { return label; }
}
//问题就是这个方法,他调用了PDestination(s)私有的构造器
public Destination dest(String s) {
return new PDestination(s); //构造器是私有的怎么可以new?
}
public Contents cont() {
return new PContents();
}
} class Test {
public static void main(String[] args) {
Parcel3 p = new Parcel3();
Contents c = p.cont();
Destination d = p.dest("Tanzania");
// Illegal -- can't access private class:
//! Parcel3.PContents c = p.new PContents();
}
}
为什么可以调用私有的构造器?
都可以在内部类中使用