在4种访问控制中,
public一般称作公共权限,其限制最小,也可以说没有限制,使用public修饰的内容可以在其它所有位置访问,只要能访问到对应的类,就可以访问到类内部public修饰的内容,一般在项目中开放的方法和构造方法使用public修饰,开放给项目使用的类也使用public修饰
protected一般称作继承权限,使用protected修饰的内容可以被同一个包中的类访问也可以在不同包内部的子类中访问,一般用于修饰只开放给子类的属性、方法和构造方法。
- 无访问控制符一般称作包权限,无访问控制符修饰的内容可以被同一个包中的类访问,一般用于修饰项目中一个包内部的功能类,这些类的功能只是辅助其它的类实现,而为包外部的类提供功能。
- private一般称作私有权限,其限制最大,类似于文件中的绝密,使用private修饰的内容只能在当前类中访问,而不能被类外部的任何内容访问,一般修饰不开放给外部使用的内容,修改private的内容一般对外部的实现没有影响。
测试代码
package com.hgh.authoritytest;
AuthorityTest
package com.hgh.authoritytest;
public class AuthorityTest {
public Integer test_public;
protected Integer test_protected;
private Integer test_private;
Integer test_default;
public AuthorityTest(Integer test_public, Integer test_protected,
Integer test_private,Integer test_deault) {
super();
this.test_public = test_public;
this.test_protected = test_protected;
this.test_private = test_private;
this.test_default = test_deault;
}
private Integer getTest_private() {
return test_private;
}
protected Integer getTest_protected() {
return test_protected;
}
Integer getTest_default() {
return test_default;
}
public Integer getTest_public() {
return test_public;
}
public static void main(String[] args) {
AuthorityTest authorityTest = new AuthorityTest(1, 2, 3,4);
/*
* 私有的只能在类内部被访问,就算在外面new了一个该类的对象,也是无法访问到private修饰的域或者方法的;
* 只要提供了public或者protected的域访问的方法(getxxx,setxxx),私有的域才能在类外部被访问到.所以要注意.
*
*/
Integer test_private = authorityTest.test_private;
Integer test_protected = authorityTest.test_protected;
Integer test_public = authorityTest.test_public;
Integer test_default = authorityTest.test_default;
authorityTest.getTest_private();
authorityTest.getTest_protected();
authorityTest.getTest_default();
authorityTest.getTest_public();
}
}
Main
package com.hgh.authoritytest;
public class Main {
public static void main(String[] args) {
AuthorityTest authorityTest = new AuthorityTest(1, 2, 3,4);
/*
* 私有的只能在类内部被访问,就算在外面new了一个该类的对象,也是无法访问到private修饰的域或者方法的;
* 只要提供了public或者protected的域访问的方法(getxxx,setxxx),私有的域才能在类外部被访问到.
* 所以要注意,如果域是可变类,那么返回了引用之后,如果在其他地方被修改了,那么对象里面的这个域也就被修改了
*
*/
//不在同一个类内部,就不能访问private修饰的域或者方法The field AuthorityTest.test_private is not visible
//Integer test_private = authorityTest.test_private;
Integer test_protected = authorityTest.test_protected;
Integer test_public = authorityTest.test_public;
Integer test_default = authorityTest.test_default;
//The method getTest_private() from the type AuthorityTest is not visible
//authorityTest.getTest_private();
authorityTest.getTest_protected();
authorityTest.getTest_default();
authorityTest.getTest_public();
}
}
测试不同的包
package com.hgh.authoritytest2;
测试不同包的继承
ProtectedTest
package com.hgh.authoritytest2;
import com.hgh.authoritytest.AuthorityTest;
public class ProtectedTest extends AuthorityTest{
public ProtectedTest(Integer test_public, Integer test_protected,
Integer test_private, Integer test_deault) {
super(test_public, test_protected, test_private, test_deault);
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
/*
* protected修饰的域和方法,可以在:同一个类内部,同一个包内部,不同包中的子类访问(在子类内部可以访问.出了子类内部也不行了),
* 不同包的非子类不能访问
*/
ProtectedTest protectedTest = new ProtectedTest(1, 2, 3,4);
/*
* 无访问控制权限:默认的权限,只能在类内部,或者同一个包内部被访问.
*/
//Integer test_private = protectedTest.test_private;
//Integer test_default = protectedTest.test_default;
Integer test_protected = protectedTest.test_protected;
Integer test_public = protectedTest.test_public;
//protectedTest.getTest_private();
//protectedTest.getTest_default();
protectedTest.getTest_protected();
protectedTest.getTest_public();
}
}
测试不同包的非子类
Main
package com.hgh.authoritytest2;
import com.hgh.authoritytest.AuthorityTest;
public class Main {
public static void main(String[] args) {
AuthorityTest authorityTest = new AuthorityTest(1, 2, 3,4);
/*
* protected修饰的域和方法,可以在:同一个类内部,同一个包内部,不同包中的子类访问(在子类内部可以访问.出了子类内部也不行了),
* 不同包的非子类不能访问
*/
// Integer test_private = authorityTest.test_private;
// Integer test_protected = authorityTest.test_protected;
// Integer test_default = authorityTest.test_default;
Integer test_public = authorityTest.test_public;
// authorityTest.getTest_private();
// authorityTest.getTest_protected();
// authorityTest.getTest_default();
authorityTest.getTest_public();
ProtectedTest protectedTest = new ProtectedTest(1, 2, 3,4);
/*
* protected修饰的域和方法,可以在:同一个类内部,同一个包内部,不同包中的子类访问(在子类内部可以访问.出了子类内部也不行了),
* 不同包的非子类不能访问
*/
//Integer test_protected2 = protectedTest.test_protected;
Integer test_public2 = protectedTest.test_public;
}
}