
//只有parcel4 及其子类 和 同包的类可以访问能访问PDestination,其他类都不能访问PDestinationclass Parcel4 { class PContents implements Contents { private int i = 11; public int value() { return i; } } protected class PDestination implements Destination { private String label; PDestination(String whereTo) { label = whereTo; } @Override public String realLabel() { return label; } } protected PDestination getPD(String s){ //19-20是为了下面的例子 return new PDestination(s); } // destination方法的返回类型是接口类型,new出来的PDestination(s)不知道他的确切类型 public Destination destination(String s) { return new PDestination(s); } public Contents contents() { return new PContents(); }}public class TestParcel { public static void main(String[] args) { Parcel4 p = new Parcel4(); Contents c = p.contents(); //note--第四行的private去掉下面这行代码就不会报错: class PContents implements Contents //! Parcel4.PContents c1 = (Parcel4.PContents) c; Destination d = p.destination("Tasmania"); // Illegal -- can't access private class: // Parcel4.PContents pc = p.new PContents(); // Parcel4.PDestination pd = p.new PDestination("sasa"); }}class Test extends Parcel4{ public static void main(String[] args) { Test t = new Test(); Parcel4 p4 = new Parcel4(); Contents c = p4.contents(); Parcel4.PDestination pd = p4.getPD("sss"); pd.realLabel(); }}class Test2{ public static void main(String[] args) { Parcel4 p = new Parcel4(); Parcel4.PDestination pd = p.getPD("sssas"); }}