你抄错了吧 应该这样: public class Parcel4 { class PDestination{ //implements Destination { private String label; private PDestination(String whereTo) {//˽Óй¹Ô캯Êý label = whereTo; } public String readLabel() { return label; } }
public PDestination dest(String s) { return new PDestination(s);//ÓÃ˽Óй¹Ô캯Êý¹¹Ôì¶ÔÏ󣿣¿£¿ÎªÊ²Ã´¿ÉÒÔÕâÑù£¿£¿£¿ } public static void main(String[] args) { Parcel4 p = new Parcel4(); PDestination d = p.dest("Tanzania"); } } 此时PDestination是个内部类,它的私有方法是允许被Parcel4调用的如果这样: public class Parcel4 {
public PDestination dest(String s) { return new PDestination(s);//ÓÃ˽Óй¹Ô캯Êý¹¹Ôì¶ÔÏ󣿣¿£¿ÎªÊ²Ã´¿ÉÒÔÕâÑù£¿£¿£¿ } public static void main(String[] args) { Parcel4 p = new Parcel4(); PDestination d = p.dest("Tanzania"); } }class PDestination{ //implements Destination { private String label; private PDestination(String whereTo) {//˽Óй¹Ô캯Êý label = whereTo; } public String readLabel() { return label; } } 即PDestination定义在外面 就不可以调用私有构造函数了
应该这样:
public class Parcel4 {
class PDestination{
//implements Destination {
private String label;
private PDestination(String whereTo) {//˽Óй¹Ô캯Êý
label = whereTo;
}
public String readLabel() { return label; }
}
public PDestination dest(String s) {
return new PDestination(s);//ÓÃ˽Óй¹Ô캯Êý¹¹Ôì¶ÔÏ󣿣¿£¿ÎªÊ²Ã´¿ÉÒÔÕâÑù£¿£¿£¿
}
public static void main(String[] args) {
Parcel4 p = new Parcel4();
PDestination d = p.dest("Tanzania");
}
}
此时PDestination是个内部类,它的私有方法是允许被Parcel4调用的如果这样:
public class Parcel4 {
public PDestination dest(String s) {
return new PDestination(s);//ÓÃ˽Óй¹Ô캯Êý¹¹Ôì¶ÔÏ󣿣¿£¿ÎªÊ²Ã´¿ÉÒÔÕâÑù£¿£¿£¿
}
public static void main(String[] args) {
Parcel4 p = new Parcel4();
PDestination d = p.dest("Tanzania");
}
}class PDestination{
//implements Destination {
private String label;
private PDestination(String whereTo) {//˽Óй¹Ô캯Êý
label = whereTo;
}
public String readLabel() { return label; }
}
即PDestination定义在外面
就不可以调用私有构造函数了