类实例化成对象之后,可以通过对象加上"."操作符访问和操纵该对象的域和方法,但是这种访问是有限制的,通过public、protected、default(啥都不写)、private来控制。
先看一个实验的例子:(不注释表示可以访问,注释掉表示无法访问)
package packageA; import packageB.SubB; public class Base { public String publicStr = "publicString"; protected String protectedStr = "protectedString"; String defaultStr = "defaultString"; private String privateStr = "privateString"; public void print() { System.out.println("packageA.Base has access to"); System.out.println(" " + publicStr); System.out.println(" " + protectedStr); System.out.println(" " + defaultStr); System.out.println(" " + privateStr); Base b = new Base(); // -- other Base instance System.out.println(" b." + b.publicStr); System.out.println(" b." + b.protectedStr); System.out.println(" b." + b.defaultStr); System.out.println(" b." + b.privateStr); SubA subA=new SubA(); System.out.println(" subA." + subA.publicStr); System.out.println(" subA." + subA.protectedStr); System.out.println(" subA." + subA.defaultStr); // System.out.println(" subA." + subA.privateStr); SubB subB=new SubB(); System.out.println(" subB." + subB.publicStr); System.out.println(" subB." + subB.protectedStr); // System.out.println(" subB." + subB.defaultStr); // System.out.println(" subB." + subB.privateStr); } }
package packageA; import packageB.SubB; public class SubA extends Base { public void print() { System.out.println("packageA.SubA has access to"); System.out.println(" " + publicStr + " (inherited from Base)"); System.out.println(" " + protectedStr + " (inherited from Base)"); System.out.println(" " + defaultStr + " (inherited from Base)"); // -- not accessible - private elements are even not inherited // System.out.println(privateStr); Base b = new Base(); // -- other Base instance System.out.println(" b." + b.publicStr); System.out.println(" b." + b.protectedStr); System.out.println(" b." + b.defaultStr); // -- not accessible // System.out.println(b.privateStr); SubA subA=new SubA(); System.out.println(" subA." + subA.publicStr); System.out.println(" subA." + subA.protectedStr); System.out.println(" subA." + subA.defaultStr); // System.out.println(" subA." + subA.privateStr); SubB subB=new SubB(); System.out.println(" subB." + subB.publicStr); System.out.println(" subB." + subB.protectedStr); // System.out.println(" subB." + subB.defaultStr); // System.out.println(" subB." + subB.privateStr); } }
package packageA; import packageB.SubB; public class AnotherA { public void print() { System.out.println("packageA.AnotherA has access to"); Base b = new Base(); System.out.println(" b." + b.publicStr); System.out.println(" b." + b.protectedStr); System.out.println(" b." + b.defaultStr); // System.out.println(b.privateStr); SubA subA=new SubA(); System.out.println(" subA." + subA.publicStr); System.out.println(" subA." + subA.protectedStr); System.out.println(" subA." + subA.defaultStr); // System.out.println(" subA." + subA.privateStr); SubB subB=new SubB(); System.out.println(" subB." + subB.publicStr); System.out.println(" subB." + subB.protectedStr); // System.out.println(" subB." + subB.defaultStr); // System.out.println(" subB." + subB.privateStr); } }
package packageB; import packageA.Base; import packageA.SubA; public class SubB extends Base { public void print() { System.out.println("packageB.SubB has access to"); System.out.println(" " + publicStr + " (inherited from Base)"); // -- protectedStr is inherited element -> accessible System.out.println(" " + protectedStr + " (inherited from Base)"); // -- not accessible // System.out.println(defaultStr); // System.out.println(privateStr); Base b = new Base(); // -- other Base instance System.out.println(" b." + b.publicStr); // -- protected element, which belongs to other object -> not accessible // System.out.println(b.protectedStr); // -- not accessible // System.out.println(b.defaultStr); // System.out.println(b.privateStr); SubA subA=new SubA(); System.out.println(" subA." + subA.publicStr); // System.out.println(" subA." + subA.protectedStr); // System.out.println(" subA." + subA.defaultStr); // System.out.println(" subA." + subA.privateStr); SubB subB=new SubB(); System.out.println(" subB." + subB.publicStr); System.out.println(" subB." + subB.protectedStr); // System.out.println(" subB." + subB.defaultStr); // System.out.println(" subB." + subB.privateStr); } }
package packageB; import packageA.Base; import packageA.SubA; public class AnotherB{ public void print() { System.out.println("packageB.AnotherB has access to"); Base b = new Base(); System.out.println(" b." + b.publicStr); // -- not accessible // System.out.println(b.protectedStr); // System.out.println(b.defaultStr); // System.out.println(b.privateStr); SubA subA=new SubA(); System.out.println(" subA." + subA.publicStr); // System.out.println(" subA." + subA.protectedStr); // System.out.println(" subA." + subA.defaultStr); // System.out.println(" subA." + subA.privateStr); SubB subB=new SubB(); System.out.println(" subB." + subB.publicStr); // System.out.println(" subB." + subB.protectedStr); // System.out.println(" subB." + subB.defaultStr); // System.out.println(" subB." + subB.privateStr); } }
总结:
1).不考虑继承的情况下:
一个类在 ”某处“实例化并访问域和方法,或者直接访问类域和类方法:
a). 在类定义出:四种类型都可访问。(见Base中的b对象)
b). 与类定义在同一包中:default、protected、public可访问。(见AnotherA中b对象)
c). 与类定义在不同包中:仅可访问public类型。(见AnotherB中b对象)
2).有继承关系
一个子类在 ”某处“实例化并访问父类继承的域和方法(子类新定义的域和方法参加上面不考虑继承的情况),或者直接访问父类类域和类方法:
a).定义在包内的子类(如上例的SubA)
A).在子类定义处、同包内:default、protected、public可访问。
B).在包外:只可访问public属性
b).定义在包外的子类(如上例的SubB)
A).在子类定义处、父类同包内:protected及public可访问。
B).在子类包内(非子类定义处):只可访问public属性
java访问权限整理
时间: 2024-10-06 08:50:13