【Thinking in Java】类的继承与protect权限
protect权限:
- 继承此方法所属的类,即可享有调用该方法的权限
- 与此类同在一个路径包下的类也可以调用此方法(相当于默认包权限)
继承(extends):
类A被类B继承,new B()的时候,A的构造方法会被调用。
1 public class ExtendsTest { 2 3 public static void main(String[] args) { 4 B b=new B(); 5 b.fun(); 6 } 7 static class A{ 8 public A() { 9 System.out.println("A Constructor"); 10 } 11 protected void fun() { 12 System.out.println("protect method"); 13 } 14 } 15 static class B extends A{ 16 public B() { 17 System.out.println("B Constructor"); 18 19 } 20 } 21 }
打印:
A Constructor
B Constructor
protect method
posted on 2015-04-12 20:26 DarkHorse_pxf 阅读(283) 评论(0) 收藏 举报
浙公网安备 33010602011771号