黑马程序员技术交流社区

标题: 遇到的一个面试题,求解答 [打印本页]

作者: youtianlong123    时间: 2014-11-12 16:55
标题: 遇到的一个面试题,求解答
有这样三个类,Person,Student.GoodStudent。其中Student继承了Person,GoodStudent继承了Student,三个类中只有默认的构造函数,用什么样的方法证明在创建Student类的对象的时候是否调用了Person的构造函数,在创建GoodStudent类的对象的时候是否调用了Student构造函数?如果在创建Student对象的时候没有调用Person的构造函数,那么采用什么样的手段可以调用父类的构造函数
作者: 李春江    时间: 2014-11-12 20:54
有点拗!!!
作者: 柳超    时间: 2014-11-12 21:06
这个要互相验证    你要证明在创建GoodStudent类的对象时在Student类中的参构造方法中打印一句话,------调用构造方法使用super关键字
作者: striker    时间: 2014-11-12 21:14
柳超 发表于 2014-11-12 21:06
这个要互相验证    你要证明在创建GoodStudent类的对象时在Student类中的参构造方法中打印一句话,------调 ...

就是这样子,入学申请时就碰到过这个,最好就是打印语句
作者: 柳超    时间: 2014-11-12 21:42
striker 发表于 2014-11-12 21:14
就是这样子,入学申请时就碰到过这个,最好就是打印语句

就是这样证明的
作者: 柳超    时间: 2014-11-12 21:43
另外    祝你成功
作者: wf111sxwf    时间: 2014-11-12 21:53
我个人觉得最直接的方法就打印一个特殊的字符串来验证
作者: cbb    时间: 2014-11-12 22:48
class Person
{
}
class Student extends Person
{               
}
class GoodStudent extends Student
{
}

class PersonDemoTest
{   
        public static void main(String[] args)
        {   
                System.out.println(new Student().getClass().getSuperclass().getName());//1
                System.out.println(new GoodStudent().getClass().getSuperclass().getName());//2
                Person p=new Student();//向上转型。       
        }
}

楼主有什么好的方法? 求告知 我这估计错了
作者: dong53821713    时间: 2014-11-12 23:46
  1. public class Demo {

  2.         public static void main(String[] args) {
  3.                 new GoodStudent();
  4.         }

  5. }
  6. class Person
  7. {
  8.         Person(){
  9.                 System.out.println("person");
  10.         }
  11. }
  12. class Student extends Person
  13. {               
  14.         Student(){
  15.                 System.out.println("student");
  16.         }
  17. }
  18. class GoodStudent extends Student
  19. {
  20.         GoodStudent(){
  21.                 System.out.println("goodstudent");
  22.         }
  23. }
复制代码



作者: relice    时间: 2014-11-13 01:37
/*第一问:
class ExtendsTest
{
        public static void main(String[] args)
        {
                //new GoodStudent();
                new Student();
        }
}
class Person
{
        Person()
        {System.out.println("Person");}
}
class Student extends Person
{
        Student()
        {System.out.println("Student");}
}
class GoodStudent extends Student
{
        GoodStudent()
        {System.out.println("GoodStudent");}
}
*/
//第二问
class ExtendsTest
{
        public static void main(String[] args)
        {
                //new GoodStudent();
                new Student();
        }
}
class Person
{
        Person()
        {System.out.println("Person");}
}
class Student extends Person
{
        Student()
        {
                super();
                System.out.println("Student");
        }
}

作者: relice    时间: 2014-11-13 02:10
//第二问修复下
class ExtendsTest
{
        public static void main(String[] args)
        {
                //new GoodStudent();
                new GoodStudent();
        }
}
class Person
{
        Person()
        {System.out.println("Person");}
}
class Student
{
        ExtendsTest e = new ExtendsTest();
        Student()
        //{System.out.println("Student");}
}
class GoodStudent extends Student
{
        GoodStudent()
        {System.out.println("GoodStudent");}
}
作者: DamonZh    时间: 2014-11-13 08:18
楼主都说了只有默认构造方法 还怎么在构造方法中写打印语句呀 亲们
作者: youtianlong123    时间: 2014-11-13 17:22
无参的构造函数写出来还能算是默认的构造函数吗?
作者: javaAndroid    时间: 2014-11-13 20:40
很简单,在需要验证的地方添加一个输出语句就行
比如在Person的构造函数里添加一句System.out.println("Person");
这样在new Student对象的时候,
如果调用了父类指定的构造函数
就会打印出这句话。否则就不会打印。
如果需要显式调用父类构造函数的话
则在子类构造函数第一行写上super();
super()是父类无参构造函数,
如果需要调用有参的就需要指定子类参数
然后在super()的括号里按对应的顺序写好参数。
作者: 焦旭宁1    时间: 2014-11-13 21:03
学习!!!!1
作者: DamonZh    时间: 2014-11-13 22:27
youtianlong123 发表于 2014-11-13 17:22
无参的构造函数写出来还能算是默认的构造函数吗?

不可以,我记得毕老师说过
作者: wf111sxwf    时间: 2014-11-13 22:50
擦 都搞错了  人家是默认构造函数不是空参构造函数。  这个问题略屌啊,  我不会 求大神指点啊
作者: M_______r    时间: 2014-11-14 13:02
public class Test09 {
       
        public static void main(String[] args) {
                //创建一个有参的GoodStudent时,只要Student类指定super(int num)就会调用Person的有参构造函数
                //GoodStudent gs = new GoodStudent(40);
                //如我我想调用Person 的有参构造函数和Student的有参构造函数 用以下函数即可证明
                GoodStudent gs1 = new GoodStudent(30);
                System.out.println(gs1);
                /*调用了Person中有参的构造函数40
                调用了Student中有参的构造函数40
                调用了GoodStudent中有参的构造函数30*/
               
               
        }
}
        class Person {
                Person() {
                        System.out.println("调用了Person中空参的构造函数");
                }

                Person(int num) {
                        System.out.println("调用了Person中有参的构造函数" + num);
                }
        }

        class Student extends Person {
                Student() {
                        System.out.println("调用了Student中空参的构造函数");
                }

                Student(int num) {
                        super(40);
                        System.out.println("调用了Student中有参的构造函数" + num);
                }

        }

        class GoodStudent extends Student {
                GoodStudent() {
                        System.out.println("调用了GoodStudent中空参的构造函数");
                }

                GoodStudent(int num) {
                        super(40);
                        System.out.println("调用了GoodStudent中有参的构造函数" + num);
                }
        }



作者: 时间都去哪了    时间: 2014-11-15 21:59
顶贴,也是一种美德
作者: 迷失的小Z    时间: 2014-11-15 22:37
在Person类中自定义一个默认构造函数 在里面输出一条语句,建立对象看是否输出语句就ok了……
作者: cvnmklop    时间: 2014-11-15 23:42
cbb 发表于 2014-11-12 22:48
class Person
{
}

你这个是错的。因为三个类的字节码已经存在并且他们的关系也明确了。你调用getSuperClass()肯定能找到
作者: cvnmklop    时间: 2014-11-16 00:57
想了半天也没想出来什么较好的解决方法。要不就在.GoodStudent Person 添加构造代码块,在构造代码块中写输出语句
作者: cbb    时间: 2014-11-16 09:07
cvnmklop 发表于 2014-11-15 23:42
你这个是错的。因为三个类的字节码已经存在并且他们的关系也明确了。你调用getSuperClass()肯定能找到 ...

默认的构造方法  还真不知道怎么弄~~




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2