protected

作者: 冯宏志 | 来源:发表于2020-11-28 19:12 被阅读0次
    Order类
    package com.atguigu.java1;
    
    public class Order {
        public int numberPublic;
        int numberDefault;
        protected int numberProtected;
        private int numberPrivate;
        
        public void methodPublic() {
            System.out.println("methodPublic");
        }
        
        void methodDefault() {
            System.out.println("methodDefault");
        }
        
        protected void methodProtected() {
            System.out.println("methodProtected");
        }
        
        private void methodPrivate() {
            System.out.println("methodPrivate");
        }
        
    }
    
    
    SubOrder类(Order不同包下的子类)
    package com.atguigu.java2;
    
    import com.atguigu.java1.*;
    
    public class SubOrder extends Order{
        public static void main(String[] args) {
            SubOrder s1 = new SubOrder();
            s1.numberPublic = 1;
            s1.numberProtected = 2;     //不同包下的子类可以调用
            //s1.numberDefault = 3;     报错:The field Order.numberDefault is not visible
            //s1.numberPrivate = 4;     报错:The field Order.numberDefault is not visible
            
            s1.methodPublic();
            s1.methodProtected();
            //s1.methodDefault();       报错:The method methodDefault() from the type Order is not visible
            //s1.methodPrivate();       报错:The method methodDefault() from the type Order is not visible
        } 
    }
    
    

    相关文章

      网友评论

          本文标题:protected

          本文链接:https://www.haomeiwen.com/subject/oqjqyktx.html