首页 > 编程语言 > 详细

Java内部类

时间:2016-11-03 16:10:09      阅读:136      评论:0      收藏:0      [点我收藏+]
/**
 * 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();
    }
}

 

Java内部类

原文:http://www.cnblogs.com/frankyou/p/6026821.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!