Java 四种访问控制权限

1.背景:

        针对java的类成员访问控制权限理解

2.Java有四种访问控制权限:

        private,protected,public,default

他们的具体访问权限都是什么呢?用例子来分析一下:

这里要分几个情景:内部访问,继承关系,同包,不同包

(1)首先我们要创建两个包:

com.test.accessrights.pk1和 com.test.accessrights.pk2  用来测试不同包之间的访问权限区别。

然后创建一个Father类作为父类,放在pk1包中,然后创建如下四个属性:

扫描二维码关注公众号,回复: 16689723 查看本文章
public class Father {
    private String param1 ="这是private";
    protected String param2 ="这是protected";
    public String param3 = "这是public";
    String param4 ="这是default";
}

然后在Father类内部测试四个属性能否访问:

public class Father {

private String param1 ="这是private";
protected String param2 ="这是protected";
public String param3 = "这是public";
String param4 ="这是default";

public static void main(String[] args) {
    Father father = new Father();
        System.out.println("father 实例访问:"+father.param1);
        System.out.println("father 实例访问:"+father.param2);
        System.out.println("father 实例访问:"+father.param3);
        System.out.println("father 实例访问:"+father.param4);}
}

代码没有编译错误,由此可见,四种类型都支持类内部访问

接下来在pk1包中建一个Child类,继承Father类,分别通过Father的对象和Child的对象访问属性

public class Child extends Father{

public static void main(String[] args) {

Father father = new Father();
System.out.println(father.param2);
System.out.println(father.param3);
System.out.println(father.param4);

Child child = new Child();
System.out.println(child.param2);
System.out.println(child.param3);
System.out.println(child.param4);

}

}

由此可见,在同一个包中,子类可以访问父类除private类型之外的类型的属性和方法

(2)那么如果不在同一个包中,子类的访问权限是否还和上面的例子一样呢?

我们在pk2包中创建一个Child2类,继承自Father类,创建一个Father的对象,访问其属性发现只能访问到param3,也就是public类型的,而其他类型的都不能访问。

那么我创建一个Child2类的对象,通过子类访问父类属性,看看如何,发现它可以访问param2,和param3,也就是protected和public类型

由此可以看出,对于private来说,只能类内访问,对于protected,目前来看,除了内部访问,也可以被子类访问,即使不同包中。而对于default,除了内部访问外,子类如果访问的话必须满足同包的条件public则目前没有限制

(3)那么还有没有别的情况呢?我们来测试一下同一包中,不是继承关系的类,会访问到什么。

在pk1包中,创建一个Stranger类,创建Father,Child,Child2对象,看看访问属性的情况。

public class Stranger {

public static void main(String[] args) {

Father father=new Father();
System.out.println("father对象访问:"+father.param2);
System.out.println("father对象访问:"+father.param3);
System.out.println("father对象访问:"+father.param4);

Child child = new Child();
System.out.println("child对象访问:"+child.param2);
System.out.println("child对象访问:"+child.param3);
System.out.println("child对象访问:"+child.param4);

Child2 child2 =new Child2();
System.out.println("child2对象访问:"+child2.param2);
System.out.println("child2对象访问:"+child2.param3);}
}

Father对象和Child对象都能访问出param1以外的其他属性,这说明同包protected满足同包中非子类访问,default也满足同包中非子类访问

而Child2对于Stranger来说不是同包的类,所以Stranger只能访问Child2的param2,和param3属性,这说明什么?

我们再在pk2中创建一个Stranger2类,用它去访问Father,Child,Child2对应的属性。

public class Stranger2 {

public static void main(String[] args) {

Father father=new Father();
System.out.println("father 实例访问:"+father.param3);
Child child = new Child();

System.out.println("child 实例访问:"+child.param3);

Child2 child2 = new Child2();
System.out.println("child2 实例访问:"+child2.param3);}
}

最后发现,只能访问param3,也就是public,至此可以知道,最起码public是没有任何限制的,而private则限制最大,那么其他两种,protected和default,有什么规则呢。

从上面的例子可以看出,在同一个包中,Father类的protected属性可以被子类访问,也可以被同包中其他类访问,在其他包中,protected属性只能在子类里通过子类对象访问,不能通过father对象访问,而在其他类里则也访问不到,在同一个包中,Father类的default属性可以被子类访问到,也可以被同一包中的其他类访问,从Stranger2类中的例子中得出结论是,default不能被不同包中的其他类访问到,而在Stranger类中却不能通过child2访问该属性,并且通过Child2类的例子,可以看出,child2对象也不能访问到Father类的default属性,说明default也不能被其他包的子类访问,所以default不能被其他包中的类访问,不管是不是子类。

【用一张表来总结】

访问控制权限(yes代表可以访问)
private default protected public
同一类 yes yes yes yes
同一包中的类 yes yes yes
子类 yes yes
其他包中的类 yes

猜你喜欢

转载自blog.csdn.net/u013302168/article/details/132483164