10.4內部類與向上轉型java
一、內部類——某個接口的實現——可以徹底不可見,而且不可用。所獲得的只是指向基類或接口的引用,因此可以很方便的隱藏實現細節。code
interface Contents { int value(); } interface Destination { String readLabel(); } class Parcel4 { private class PContents implements Contents { private int i = 11; public int value() { return i; } } protected class PDestination implements Destination { private String label; public PDestination (String whereTo) { label = whereTo; } public String readLabel() { return label; } } 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(); Destination d = p.destination("xiao"); } }