关于 Java 继承的一些复习
2009-06-18 09:51 hjhnju 51CTO“就像以往” 博客 我要评论(1) 字号:T | T
本文总结了 Java 继承中的几点心得,其中包括继承后成为私有域的情况,子类构造函数没有显式调用超
类构造函数,以及超类没有无参构造函数,子类中又没有显式调用的情况等等。
AD:
以下,作者讲述了在复习 Java 继承时的一些心得。看一段简单的代码
1. publicclassTest{
2. publicstaticvoidmain(String[]args){
3. Bb=newB(0);
4. inty=b.getY();
5. }
6.
7. }
8.
9. classA{
10. publicstaticintx=2;//1.
11. privateinty=2;//2.
12. protectedintz;//5.
13. A(){//3.
14. x=x+1;
15. showX();//4.
16. }
17. publicvoidshowX(){
18. System.out.println("A.x="+x);
19. }
20. publicintgetY(){
21. returny;
22. }
23.
24. }
25.
26. classBextendsA{
27.
28. B(intx){
29. x=x+2;//只对局部 x 操作
30. showX();
31. }
32. publicvoidshowX(){