外部类有一个方法,该方法返回一个指向内部类的引用

    1. public class Parcel2 {
    2. class Contents {
    3. private int i = 11;
    4. public int value() { return i; }
    5. }
    6. class Destination {
    7. private String label;
    8. Destination(String whereTo) {
    9. label = whereTo;
    10. }
    11. String readLabel() { return label; }
    12. }
    13. public Destination to(String s) {
    14. return new Destination(s);
    15. }
    16. public Contents contents() {
    17. return new Contents();
    18. }
    19. public void ship(String dest) {
    20. Contents c = contents();
    21. Destination d = to(dest);
    22. System.out.println(d.readLabel());
    23. }
    24. public static void main(String[] args) {
    25. Parcel2 p = new Parcel2();
    26. p.ship("Tasmania");
    27. // 定义对内部类的引用:
    28. Parcel2.Contents c = p.contents();
    29. Parcel2.Destination d = p.to("adfs");
    30. }
    31. }