zoukankan      html  css  js  c++  java
  • 权限修饰符(public、protected、default、private)权限验证

    一、测试方法

    authorityTest包中建类AuthorityValidate
    package authorityTest;
    
    
    public class AuthorityValidate {
        private String authority1 = "private";
        protected String authority2 = "protected";
        private void runPrivate(String a) {
            System.out.print(a);
        }
        protected void runProtectde(String b) {
            System.out.print(b);
            System.out.print(authority1);
        }
    }

    在authorityTest包中建立TheThirdClass类并尝试调用AuthorityValidate 中不同权限的方法

    package authorityTest;
    
    import testRun.ChildOfAut;
    
    
    public class TheThirdClass {
        ChildOfAut a = new ChildOfAut();
        AuthorityValidate b = new AuthorityValidate();
        String c = b.authority2;
        String d = a.authority2;
        public static void main(String[] args) {
            AuthorityValidate b = new AuthorityValidate();
            ChildOfAut a = new ChildOfAut();
            System.out.print(b.authority2);
            System.out.print(a.authority2);
        }
    }

    在testRun包中建立ChildOfAut类继承AuthorityValidate 并尝试调用AuthorityValidate 中不同权限的方法

    package testRun;
    
    import authorityTest.AuthorityValidate;
    
    
    public class ChildOfAut extends AuthorityValidate {
        
        public static void main(String[] args) {
            ChildOfAut a = new ChildOfAut();
            System.out.print(a.authority2);
            a.runProtectde("果然如此");
        }
    }

    在testRun包中建立TheOtherPackage类并尝试调用AuthorityValidate 中不同权限的方法

    package testRun;
    
    import authorityTest.AuthorityValidate;
    
    
    public class TheOtherPackage {
        AuthorityValidate b = new AuthorityValidate();
        String a = b.authority2;
    }


    二、测试结果
    TheThirdClass 类能调用AuthorityValidate 中protected修饰的属性和方法;
    ChildOfAut类能调用AuthorityValidate 父类protected修饰的属性和方法;
    TheOtherPackage类无法调用AuthorityValidate 父类protected修饰的属性和方法;
    TheThirdClass,ChildOfAut,TheOtherPackage都不能访问AuthorityValidate 的private方法及属性;
    以上,证明被protected修饰的属性、方法可被同一包中的类以及其子类访问。
    被private修饰的属性、方法只能自身访问;
    同理可证public可被任意类访问、default可被同一包中的类访问。
     
  • 相关阅读:
    NS3系列—4———NS3中文教程5:Tweaking NS3
    NS3系列—3———NS3中文:4 概念描述
    NS3系列—2———NS3笔录
    NS3系列—1———NS3中文教程:3下载及编译软件
    How to speed my too-slow ssh login?
    Linux bridge
    使用 GDB 和 KVM 调试 Linux 内核与模块
    How To Set Up A Serial Port Between Two Virtual Machines In VirtualBox
    Linux内核调试环境搭建(基于ubuntu12.04)
    Android
  • 原文地址:https://www.cnblogs.com/qcxdoit/p/9279286.html
Copyright © 2011-2022 走看看