Java 访问控制权限
二个包
包:com.pag1
类:Base
package com.pag1;
public class Base { public int a = 1; String b = "12"; protected int c = 1; private int d = 4; public void test(){ System.out.println(this.getClass()+":"+this.a); } }
类: Derived
package com.pag1;
public class Derived extends Base{ public int a = 2; public void test2(){ System.out.println(b); } public static void main(String[] args) { new Derived().test2(); } }
Derived 能继承Base的除d(private修饰)以外的所有成员变量;
包:com.pag2
package com.pag2;
public class Derived2 extends Base { }
Derived2在和Base不在同一个包,能继承c(protected)和 a(public)修饰的成员变量,不能继承默认和private修饰的成员变量。
public class Base { public int a = 1;
String b = "12";
protected int c = 1;
private int d = 4; public void test(){
System.out.println(this.getClass()+":"+this.a); }
}
欢迎关注Java流水账公众号