Java内部类

/**
 * Created by xfyou on 2016/11/3.
 * Java内部类演示
 * 内部类可完全进入不可见或不可用状态——对任何人都将如此。所以我们可以非常方便地隐藏实施细节
 */
public class Parcel3 {
    /**
     * 私有的内部类
     */
    private class PContents extends Contents {
        private int i = 11;

        @Override
        public int value() {
            return i;
        }
    }

    /**
     * 受保护的内部类
     */
    protected class PDestination implements Destination {
        private String lable;

        private PDestination(String whereTo) {
            this.lable = whereTo;
        }

        @Override
        public String readLabel() {
            return lable;
        }
    }

    public Destination dest(String s) {
        return new PDestination(s);
    }

    public Contents cont() {
        return new PContents();
    }
}

abstract class Contents {
    abstract public int value();
}

interface Destination {
    String readLabel();
}


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();
    }
}
原文地址:https://www.cnblogs.com/frankyou/p/6026821.html