1. class Father{
2. private String name ="father" ;
3. int age =38;
4. public Father(){
5. System. out.println("Non-parametric construction method of father." );
6. System. out.println(name +" " +age );
7. System. out.println("--------------------------------------------------" );
8. }
9. public Father(String name ,int age){
10. this.name =name ;
11. this.age =age ;
12. System. out.println("Parametric construction method of father." );
13. System. out.println(name +" " +age );
14. System. out.println("--------------------------------------------------" );
15. }
16. public String getName(){
17. return name ;
18. }
19. public int getAge(){
20. return age ;
21. }
22.
23. }
24.
25. class Son extends Father{
26. private String name ="son" ;
27. int age =18;
28. public Son(){
29. System. out.println("Non-parametric construction method of son." );
30. System. out.println(name +" " +age );
31. System. out.println("--------------------------------------------------" );
32. }
33. public Son(String name, int age ){
34. super( name, age);
35. this. name= name;
36. this.age =age ;
37. System. out.println("Parametric construction method of son." );
38. System. out.println(name +" " +age );
39. System. out.println("--------------------------------------------------" );
40. }
41. public String getName(){
42. return name ;
43. }
44. public int getAge(){
45. return age ;
46. }
47. }
48.
49.
50. public class hello {
51. public static void main(String[]args){
52. Son p1= new Son); //新建一个Son对象,调用Son的无参构造方法,在此之前先访问父类的无参构造方法(对应运行结果1,2,3,4,5,6行)
53. Son p2= new Son("Paul" ,17);//新建一个Son对象,调用Son的含参构造方法,在此之前先访问父类的含参构造方法(7,8,9,10,11,12行) Question:为什么父类中打印出来的年龄和姓名变成了子类的年龄
54. p1.getName(); 和姓名? 并且即使把代码中9,10行注释掉运行结果也还是这样?不应该只是完成父类参数的初始化吗?还是程序的编写哪里出错了?
55. p1.getAge(); //调用P1,P2的getName和getAge方法并打印出来(13行)
56. p2.getName();
57. p2.getAge();
58. System. out.println(p1 .getName()+" "+ p1.getAge()+ " "+p2 .getName()+" "+p2.getAge());
59. Father e=new Father();//新建一个father对象并调用它的getName和getAge方法并打印出来//这里为了区别构造方法和打印出来的age值,给打印的age+1
60. e.getName(); //这里之所以打印出来的还是38+1是因为新建了一个father对象所以新开辟了一片堆空间储存Name和Age值
61. e.getAge();
62. System. out.println(e .getName()+" "+ ( e.getAge()+1));
63. }
64. }
运行结果:
1. Non-parametric construction method of father.
2. father 38
3. --------------------------------------------------
4. Non-parametric construction method of son.
5. son 18
6. --------------------------------------------------
7. Parametric construction method of father.
8. Paul 17
9. --------------------------------------------------
10. Parametric construction method of son.
11. Paul 17
12. --------------------------------------------------
13. son 18 Paul 17
14. Non-parametric construction method of father.
15. father 38
16. --------------------------------------------------
17. father 39
问题: 1,为什么父类中打印出来的年龄和姓名变成了子类的年龄 和姓名? 并且即使把代码中9,10行注释掉运行结果也还是这样?不应该只是完成父类参数的初始化吗?还是程序的编写哪里出错了?
2,子类中用super(...)访问父类的带参构造方法有什么意义?跟访问无参方法有何区别? 照我的运行结果来说难道子类使用super(...)访问了父类的带参构造方法,子类就会将传递给它的参数再传递给父类让父类完成初始化吗?
目前的只是储备:看了一点书的视频,刚看完接口,关于JAVA的内存问题视频教程中略有涉及,看的书中尚未涉及,看过几篇讲JAVA内存问题的帖子,不过大概知道JAVA内存的三个区的划分,但是还是不很了解,很多时候比较模糊,特别是关于构造方法到底做些什么非常不了解 非常谢谢各位Orz