学习总结:
链接:【Java基础、springboot、springcloud、docker 等,学习目录】
权限修饰符:
控制类、方法、属性的可见性。从大到小:public(公共)、protected(受保护)、dafault(缺省)、private(私有)。
一、四种修饰符在哪些范围内可见:
| public | protected | default | private | |
|---|---|---|---|---|
| 同一类中 | true | true | true | true |
| 同一包中,子类和无关类 | true | true | true | false |
| 不同包中的子类 | true | true | false | false |
| 不同包中的无关类 | true | false | false | false |
权限修饰符作用范围分为修饰类、成员两种:
修饰类:
1、类的修饰符只有public 、default两种(成员内部类可以使private)。
2、public类可被所有访问、default可被同一包中其他类访问。
修饰类成员(变量、方法):(面试)
1、成员使用private修饰只在本类中使用。
2、如果一个成员没有使用任何修饰符,默认为default,该成员可以被包中其他类访问(同一包中可以访问)。
3、public 修饰的成员可被所有类访问。
4、protected修饰成员:
参考菜鸟教程:
- 子类与基类在同一包中: 被声明为 protected 的变量、方法和构造器能被同一个包中的任何其他类访问;
- 子类与基类不在同一包中: 那么在子类中,子类实例可以访问其从基类继承而来的 protected 方法,而不能访问基类实例的protected方法。
说人话:父类中protected修饰的成员可以被同一包中其他类访问,位于不同包中的子类也可以访问(其他包中的子类,且只能在此子类中访问(使用 this· 访问),在其他类中创建此子类实例也访问不了)。
二、Demo(protected):
包结构:

包 ron1:
// 父类 :
package com.coolron.user.ron1;
public class Father {
protected String protectedVariable = "father variable";
protected void protectedMethod(){
System.out.println("Father: 受保护的方法...");
}
}
// 子类:
public class Son1 extends Father {
public void innerSon1Method(){
Father father = new Father();
father.protectedMethod();
String fatherVariable = father.protectedVariable;
System.out.println("fatherVariable: " + fatherVariable);
/*********************************************************/
this.protectedMethod();
String sonVariable = this.protectedVariable;
System.out.println("sonVariable: " + sonVariable);
}
}
// 测试:同一包中访问正常
public static void main(String[] args) {
Father father = new Father();
Son1 son1 = new Son1();
father.protectedMethod(); // Father: 受保护的方法...
String fatherVariable = father.protectedVariable;
System.out.println("fatherVariable: " + fatherVariable); // fatherVariable: father variable
/*********************************************************/
son1.protectedMethod(); // Father: 受保护的方法...
String sonVariable = son1.protectedVariable;
System.out.println("sonVariable: " + sonVariable); // sonVariable: father variable
}
不同 包 ron2:
// 不同包中的子类:
public class Son2 extends Father {
public void InnerSon2Method(){
Father father = new Father();
father.protectedMethod(); // Compile Error
String fatherVariable = father.protectedVariable; // Compile Error
System.out.println("fatherVariable: " + fatherVariable);
/*********************************************************/
this.protectedMethod(); // Compile Ok
String sonVariable = this.protectedVariable; // Compile Ok
System.out.println("sonVariable: " + sonVariable);
}
}
// 测试不同包:
public static void main(String[] args) {
Father father = new Father();
Son1 son1 = new Son1();
father.protectedMethod(); // Compile Error
String fatherVariable = father.protectedVariable; // Compile Error
/*********************************************************/
son1.protectedMethod(); // Compile Error
String sonVariable = son1.protectedVariable; // Compile Error
new Son2().InnerSon2Method();
}
特别注意:
在父类中使用protected 修饰的成员,在不同包类中创建父类实例也访问不了。
附:
java中的7中修饰符:权限(4种)、final、abstract、static
1、成员方法:7种均可。
2、成员变量:6种(除abstract)。
3、构造方法:权限4种。
4、类:public、default、final、abstract。
5、局部变量:final。
final修饰类:
当一个类不可能有子类时(final类是不可以被继承的)可以用修饰符 final把它说明为最终类。被定义为 final类通常是一些有固定作用、用来完成某种标准功能的类。
1080




被折叠的 条评论
为什么被折叠?



