• JAVA 的四种访问权限


    Java编程中,访问权限是非常重要的概念,因为它可以保证代码的安全性和封装性。访问权限有四种,分别是public、protected、default和private。

    在这里插入图片描述
    private:如果一个类的方法或者变量被private修饰,那么这个类的方法或者变量只能在该类本身中被访问,在类外以及其他类中都不能显示地进行访问。

    default默认访问权限):如果一个类的方法或变量被包访问权限修饰,也就意味着只能在同一个包中的其他类中显示地调用该类的方法或者变量,在不同包中的类中不能显示地调用该类的方法或变量。

    protected:如果一个类的方法或者变量被protected修饰,对于同一个包的类,这个类的方法或变量是可以被访问的。对于不同包的类,只有继承于该类的类才可以访问到该类的方法或者变量。

    public:被public修饰的方法或者变量,在任何地方都是可见的。

    private

    package com.ymqx.访问权限;
    
    public class Person {
        private String name = "test";
    
        public String getName(){
            //只能在本类中被访问
            System.out.println(this.name);
            return this.name;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    package com.ymqx.访问权限;
    
    public class Test {
        public static void main(String[] args){
            Person person = new Person();
    
            //Person类外不能直接访问
            //System.out.println(person.a);
            person.getName();
        }
    }
    
    输出:test
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    方法或者变量被private修饰,只能在本类中被访问。

    default

    package com.ymqx.访问权限;
    
    public class Person {
        String name = "test";
    
        public String getName(){
            System.out.println(this.name);
            return this.name;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    package com.ymqx.访问权限;
    
    public class Test {
        public static void main(String[] args){
            Person person = new Person();
    
            //同一个包中的其他类中显示地调用
            System.out.println(person.name);
            person.getName();
        }
    }
    
    输出:
    test
    test
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    同一个包中的其他类中显示地调用。

    在这里插入图片描述

    package com.ymqx.访问权限2;
    
    import com.ymqx.访问权限.Person;
    
    public class Test{
        public static void main(String[] args){
            Person person = new Person();
    
            //不同的包,Person类不能显示地调用name
            System.out.println(person.name);
            person.getName();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    不同包中的类中不能显示地调用。

    protected

    package com.ymqx.访问权限;
    
    public class Person {
        protected String name = "test";
    
        public String getName(){
            System.out.println(this.name);
            return this.name;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    package com.ymqx.访问权限2;
    
    import com.ymqx.访问权限.Person;
    
    public class Zi extends Person {
        public void getPersonName() {
            //不同的包,继承可以访问父类protected修饰变量
            System.out.println(super.name);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    不同包的类,只有继承于该类的类才可以访问到该类 protected 修饰方法或者变量。

    public

    package com.ymqx.访问权限;
    
    public class Person {
        public String name = "test";
    
        public String getName(){
            System.out.println(this.name);
            return this.name;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    package com.ymqx.访问权限2;
    
    import com.ymqx.访问权限.Person;
    
    public class Test{
        public static void main(String[] args){
            Person person = new Person();
    
            //同一个包中的其他类中显示地调用
            System.out.println(person.name);
            person.getName();
        }
    }
    
    输出:
    test
    test
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    不同包中的类也可以显示地调用。

    小结

    • 四种访问权限从小到大:private < default(默认访问权限)< protected < public。
    • 当定义变量时不加任何修饰会默认为默认权限。
    • 在子类中访问父类中的变量时需要用super。
  • 相关阅读:
    家庭实验室系列文章-如何迁移树莓派系统到更大的 SD 卡?
    Unity 6 是下一个 LTS 版本即将发布
    监控数据的采集方式及原理
    SQL抛出数据集用游标单条逐一执行
    Qt 5.9.8 安装教程
    web前端期末大作业——仿小米商城电商平台(6页) html+css+javascript网页设计实例 企业网站制作
    Java提高与实践
    DEPRECATION: The default format will switch to columns in the future
    spring ioc的循环依赖问题
    webgl(three.js)3D光伏,3D太阳能能源,3D智慧光伏、光伏发电、清洁能源三维可视化解决方案——第十六课
  • 原文地址:https://blog.csdn.net/weixin_40017062/article/details/132790301