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

  1. public class Person {
  2.         private String name;
  3.         
  4.         public Person() {
  5.                 System.out.println("Person 默认构造方法执行");        
  6.         }
  7.         public Person(String name) {
  8.                 this.name = name;
  9.                 System.out.println("Person 带参数构造方法执行 :" + name);        
  10.         }
  11.         

  12. }

  13. class Student extends Person{
  14.         private String name;
  15.         public Student() {
  16.                 System.out.println("Student 默认构造方法执行");        
  17.         }
  18.         public Student(String name) {
  19.                 this.name = name;
  20.                 System.out.println("Student 有参构造方法执行 :" + name);        
  21.         }
  22. }

  23. class GoodStudent extends Student{
  24.         public GoodStudent() {
  25.                 super("name"); //在子类的构造方法的最前面可以调用父类的构造方法。super(),参数可以为空,默认调用父类的无参构造方法
  26.                 System.out.println("GoodStudent 默认构造方法执行");
  27.         }
  28.         
  29.         public static void main(String[] args) {
  30.                 GoodStudent goodStudentg = new GoodStudent();
  31.         }        
  32. }
复制代码

有这样三个类,Person、Student、GoodStudent。
*其中GoodStudent继承于Student,Student继承于Person。
*如何证明创建GoodStudent时是否调用了Person的构造函数?
*在GoodStudent中是否能指定调用Student的哪个构造函数?
*在GoodStudent中是否能指定调用Person的哪个构造函数?



以下代码:对吗?

7 个回复

正序浏览
晴儿 中级黑马 2014-7-10 14:16:43
8#
new Goodstudent时可以通过传递参数证明是调用哪个构造函数
回复 使用道具 举报
hxwo211314 发表于 2014-7-10 13:51
可以打印出来Person类中的构造函数,就证明了GoodStudent调用了Person的构造函数 ...

嗯,谢谢,了然!
回复 使用道具 举报
沐小北 发表于 2014-7-10 13:39
我做的,對嗎?可以打印出來的。

可以打印出来Person类中的构造函数,就证明了GoodStudent调用了Person的构造函数
回复 使用道具 举报
不是很懂  问问同学后给你答复
回复 使用道具 举报
hxwo211314 发表于 2014-7-10 13:37
这是我做的,你参考一下吧

我做的,對嗎?可以打印出來的。
回复 使用道具 举报
  1. /**
  2. * 9、 有这样三个类,Person、Student、GoodStudent。
  3. *        其中GoodStudent继承于Student,Student继承于Person。
  4. *        如何证明创建GoodStudent时是否调用了Person的构造函数?
  5. *        在GoodStudent中是否能指定调用Student的哪个构造函数?
  6. *        在GoodStudent中是否能指定调用Person的哪个构造函数?
  7. *答:如果,创建GoodStudent对象,可以打印出来Person类中定义的语句,及证明了调用了Person的构造函数
  8. *        在GoodStudent中是可以指定调用Student的那个构造函数,也可以指定调用Person的那个构造函数
  9. *        想要调用指定的构造函数,可以用super(),也可以实例化对象
  10. * @author
  11. *
  12. */
  13. public class Test9 {
  14.         public static void main(String[] args) {
  15.                 //实例化GoodStudent对象
  16.                 GoodStudent gs = new GoodStudent();
  17.         }
  18. }
  19. //创建Person,Student,GoodStudent类,并指明继承关系
  20. class Person {
  21.         //分别调用不同参数的函数
  22.         //无参构造
  23.         Person () {
  24.                 System.out.println("这是Person的无参构造函数");
  25.         }
  26.         Person(String str) {
  27.                 System.out.println("这是含有一个参数的Person的构造函数");
  28.         }
  29.         Person(String str,String str1){
  30.                 System.out.println("这是含有两个参数的Person的构造函数");
  31.         }
  32. }
  33. class Student extends Person{
  34.         Student(){
  35.                 System.out.println("这是Student类的无参构造函数");
  36.         }
  37.         Student(String str){
  38.                 System.out.println("这是含有一个参数的Student类的构造函数");
  39.         }
  40.         Student(String str,String str1){
  41.                 System.out.println("这是含有两个参数的Student类的构造函数");
  42.         }
  43. }
  44. class GoodStudent extends Student{
  45.         GoodStudent(){
  46.                 super();
  47.                 System.out.println("GoodStudent");
  48.                 //实例化Person对象
  49.                 Person p = new Person("First Student");
  50.                 //实例化Student对象
  51.                 Student s = new Student("First Student","Second Student");
  52.         }
  53. }
复制代码



这是我做的,你参考一下吧

评分

参与人数 1技术分 +1 收起 理由
淡夜清风 + 1 赞一个!

查看全部评分

回复 使用道具 举报
又是这题,抽到这题的同学不少啊

你应该把输出的打印结果也贴出来
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马