protected和default

// a包
public class Student{
    public void eat(){
        System.out.println("吃饭");
        super.action();
    }
    private void exam(){
        System.out.println("学生在考试");
    }
    protected void cadre(){
        System.out.println("竞选干部");
    }
    void study(){
        System.out.println("学习");
    }
}

// b包
public class XiaoMing extends Student {
    public XiaoMing() {
        super.cadre();
        super.action();
        // 不同包调用protected只能在子孙类中
        new 基类的子孙类().protected方法();
    }
}
// b包
public class MyClass {
    public static void main(String[] args) {
        new XiaoMing().cadre(); // 因为MyClass类和People类不同包所以xiaoMing的实例不可以调用protected方法
    }
}

protected、default同包下和public并无区别,default只能在同包下访问

posted @ 2021-02-19 22:20  火鸡的呐喊  阅读(48)  评论(0编辑  收藏  举报