标签:
了解了包的概念,就可以系统的介绍Java中的访问控制级别。在Java中,针对类、成员方法和属性提供了四种访问级别,分别是private、default、protected和public。
public(公共) | protected(受保护) | default(缺省) | private(私有) | |
同一个类 | √ | √ | √ | √ |
同一个包 | √ | √ | √ | × |
子父类 | √ | √ | × | × |
不同包 | √ | × | × | × |
package d; class Demo7 { protected int a = 1 ; }
package d; public class Demo8 { public static void main(String[] args) { Demo7 z = new Demo7(); System.out.println("a="+z.a); } }运行结果:
package d; class Demo7 { int a = 1 ; }
package d; public class Demo8 { public static void main(String[] args) { Demo7 z = new Demo7(); System.out.println("a="+z.a); } }运行结果:
package d; class Demo7 { protected int a = 1 ; }
package d; public class Demo8 extends Demo7 { public static void main(String[] args) { Demo8 z = new Demo8(); System.out.println("a="+z.a); } }运行结果:
package d; class Demo7 { int a = 1 ; }
package e; import d.*; public class Demo8 extends Demo7 { public static void main(String[] args) { Demo8 z = new Demo8(); System.out.println("a="+z.a); } }运行结果:
package d; class Demo7 { protected int a = 1 ; }
package e; import d.*; public class Demo8{ public static void main(String[] args) { Demo7 z = new Demo7(); System.out.println("a="+z.a); } }运行结果:
package d; class Demo7 { int a = 1 ; }
package e; import d.*; public class Demo8{ public static void main(String[] args) { Demo7 z = new Demo7(); System.out.println("a="+z.a); } }运行结果:
标签:
原文地址:http://blog.csdn.net/thescript_j/article/details/50922869