A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 原子弹 中级黑马   /  2014-3-26 00:09  /  1311 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

有这样三个类,Person,Student,GoodStudent.
      GoodStudent继承于Student,Student继承Person.
如何证明创建GoodStudent时是否调用了Person的构造函数?
在GoodStudent中是否能指定调用Student的那个构造函数?
在GoodStudent中是否能指定调用Person的那个构造函数?
  求大神指点啊!!!

评分

参与人数 1技术分 +1 收起 理由
朱神必 + 1

查看全部评分

5 个回复

倒序浏览
在父类的构造函数中输出一句话就好,看创建子类的对象的时候那句话是否会输出来

评分

参与人数 1技术分 +1 收起 理由
朱神必 + 1

查看全部评分

回复 使用道具 举报
  1. class Person{
  2.         String name;
  3.         Person(){
  4.                 System.out.println("Pserson constructor...");
  5. }
  6. }
  7. class Student extends Person{
  8.         String id;
  9.         Student(){
  10.                 System.out.println("Student constructor...");
  11.         }
  12. }
  13. class GoodStudent extends Student{
  14.         String gender;
  15.         GoodStudent(){
  16.                 System.out.println("GoodStudent constructor...");
  17.         }
  18. }
  19. public class Test {

  20.         public static void main(String[] args) {

  21.                 GoodStudent gs = new GoodStudent();
  22.                 /*打印结果如下:
  23.                  * Pserson constructor...
  24.                  * Student constructor...
  25.              * GoodStudent constructor...
  26.              * 说明按Person->Student->GoodStudent顺序调用构造方法
  27.              * */
  28.         }
  29. }
复制代码



回复 使用道具 举报
  1. class Person{
  2.         String name;
  3.         Person(){
  4.                 System.out.println("Pserson constructor...");
  5.         }
  6.         Person(String name){
  7.                 this.name = name;
  8.                 System.out.println("Pserson constructor..."+this.name);
  9.         }
  10. }
  11. class Student extends Person{
  12.         String id;
  13.         Student(){
  14.                 System.out.println("Student constructor...");
  15.         }
  16.         Student(String name, String id){
  17.                 [color=YellowGreen]super(name);//通过这种方式指定,如果指定不带参的构造方法就写super();[/color]
  18.                 this.id = id;
  19.                 System.out.println("Student constructor..."+name+ id);
  20.         }
  21. }
  22. class GoodStudent extends Student{
  23.         String gender;
  24.         GoodStudent(){
  25.                 System.out.println("GoodStudent constructor...");
  26.         }
  27.         GoodStudent(String name, String id,String gender){
  28.                 super(name, id);//通过这种方式指定
  29.                 this.gender = gender;
  30.                 System.out.println("GoodStudent constructor..."+name + id + gender);
  31.         }
  32. }
  33. public class Test {

  34.         public static void main(String[] args) {

  35.                 GoodStudent gs = new GoodStudent("张三", "20140101", "女");
  36.         }
  37. }
复制代码

评分

参与人数 1技术分 +2 收起 理由
朱神必 + 2

查看全部评分

回复 使用道具 举报
在GoodStudent中是否能指定调用Student的那个构造函数?
  能   通过super();
在GoodStudent中是否能指定调用Person的那个构造函数?
这个能   是不是不能   必须通过Student类调用?
回复 使用道具 举报
1, 如何证明创建GoodStudent时是否调用了Person的构造函数?
执行new GoodStudent()会调用默认构造函数
你可以在Person中写一个默认构造函数执行后你看有打印Person()就知道是否调用Person构造函数
public Person(){
        System.out.println(“Person()”);
}
2,在GoodStudent中是否能指定调用Student的那个构造函数?(当然可以)
既然是想调用Student的那个构造函数,肯定有类似下面的构造函数
Student(){……}, Student(int age){……}, Student(String name,int age){……}
你只需要GoodStudent(){
                Super();//会调用Student(){……},
}
GoodStudent(parameters){
                Super(parameters);//会调用Student(int age){……}
}
GoodStudent(parameters1, parameters2){
                Super(parameters1, parameters2);//会调用Student(String name,int age){……}
}
3,在GoodStudent中是否能指定调用Person的那个构造函数?
直接在GoodStudent中是不能指定调用Person的那个构造函数
但是可以间接的指定
你可以传一个状态值到Student中,然后再Student中选择掉用Person哪一个构造函数
下面我就不唉排的列举了,和上面的原理是一样的

评分

参与人数 1技术分 +2 收起 理由
朱神必 + 2

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马