qq_28458773 2016-06-09 13:22 采纳率: 0%
浏览 1212

[JAVA真的非常非常新的新手]关于继承的构造方法的问题



    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

  • 写回答

5条回答 默认 最新

  • lzj0327 2016-06-09 13:43
    关注
    问题就在调用父类有参数构造方法,这个应该确实会将参数传到父类并初始化里面的变量
    public Son(String name, int age ){
          super( name, age);
                假如不想将变量传过去就去掉这一句,会默认调用父类无参数的构造方法
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64