Topic: 在构造方法时虚方法的调用出现错误!--为什么此时的school尚未赋值??

  Print this page

1.在构造方法时虚方法的调用出现错误!--为什么此时的school尚未赋值?? Copy to clipboard
Posted by: 科大梦之队
Posted on: 2005-09-30 21:37

class ConstructInvokeMetamorph{
  public static void main(String[] args){
  Person s=new Person("Liming",18);  
  Student p=new Student("Liming",18,"beida");

  }
}
class Person{
  String name="noname";
  int age=-1;
  Person(String name,int age){
    this.name=name;this.age=age;
    sayHello();
  }
  void sayHello(){
    System.out.println("I am a man,and my name is"+this.name+".My age is "+this.age);
  }
}
class Student extends Person{
  String school="noschool";
  Student(String name,int age,String school){
    super(name,age);
    this.school=school;
  }
  void sayHello(){
    System.out.println("I'm a student,and my name is"+this.name+".My age is "+this.age+
    ",and my school is "+this.school);
  }
}
我预想的结果是:
I am a man,and my name is Liming.My age is 18.
I'm a student,and my name is Liming.My age is 18,and my school is beida
但是结果是:
I am a man,and my name is Liming.My age is 18.
I'm a student,and my name is Liming.My age is 18,and my school is null
程序中哪里出现了错误?好像是方法的调用时,school尚未赋值,请问这是怎么回事?它怎样才能赋值,这个程序的执行过程是怎么样?请高手不吝赐教!!

2.Re:在构造方法时虚方法的调用出现错误!--为什么此时的school尚未赋值?? [Re: 科大梦之队] Copy to clipboard
Posted by: 科大梦之队
Posted on: 2005-09-30 21:45

如果我在
class ConstructInvokeMetamorph{
public static void main(String[] args){
Person s=new Person("Liming",18);
Student p=new Student("Liming",18,"beida");
p.sayHello();//加了一句!
............
输出结果为:
I am a man,and my name is Liming.My age is 18.
I'm a student,and my name is Liming.My age is 18,and my school is null
I'm a student,and my name is Liming.My age is 18,and my school is beida
我对此感到迷惑,怎么会这样呢?这个程序的执行过程是怎么样呢?请指点一下!

3.Re:在构造方法时虚方法的调用出现错误!--为什么此时的school尚未赋值?? [Re: 科大梦之队] Copy to clipboard
Posted by: 科大梦之队
Posted on: 2005-09-30 21:52

如果我在
class ConstructInvokeMetamorph{
public static void main(String[] args){
Person s=new Person("Liming",18);
Student p=new Student("Liming",18,"beida");
p.sayHello();//
s.sayHello();//又加了一句!
............
输出结果为:
I am a man,and my name is Liming.My age is 18.
I'm a student,and my name is Liming.My age is 18,and my school is null
I'm a student,and my name is Liming.My age is 18,and my school is beida
I am a man,and my name is Liming.My age is 18.
结果输出的顺序也很有意思,这个我也理解不了啊!

4.Re:在构造方法时虚方法的调用出现错误!--为什么此时的school尚未赋值?? [Re: 科大梦之队] Copy to clipboard
Posted by: andykid
Posted on: 2005-10-02 20:53

在Student类中将school定义为static后试试?

5.Re:在构造方法时虚方法的调用出现错误!--为什么此时的school尚未赋值?? [Re: 科大梦之队] Copy to clipboard
Posted by: bluecrystal
Posted on: 2005-10-02 21:39

class ConstructInvokeMetamorph{
  public static void main(String[] args){
  Person s=new Person("Liming",18);  
  Student p=new Student("Liming",18,"beida");

  }
}
class Person{
  String name="noname";
  int age=-1;
  Person(String name,int age){
    this.name=name;this.age=age;
    sayHello();
  }
  void sayHello(){
    System.out.println("I am a man,and my name is"+this.name+".My age is "+this.age);
  }
}
class Student extends Person{
  String school="noschool";
  Student(String name,int age,String school){
    super(name,age);
//调用父类的构造函数的时候,school还没有初始化成noschool;
//若把school照着andykid所说,加上static限定符,则在调用父类构造函数前,school会被先初始化,try it

    this.school=school;
  }
  void sayHello(){
    System.out.println("I'm a student,and my name is"+this.name+".My age is "+this.age+
    ",and my school is "+this.school);
  }
}

6.Re:在构造方法时虚方法的调用出现错误!--为什么此时的school尚未赋值?? [Re: bluecrystal] Copy to clipboard
Posted by: wanghcb
Posted on: 2005-10-02 22:57

还有一点是,在执行到
super(name,age);
的时候其中的
sayHello();
被子类overridding了,换个名字试试看?

7.Re:在构造方法时虚方法的调用出现错误!--为什么此时的school尚未赋值?? [Re: 科大梦之队] Copy to clipboard
Posted by: 科大梦之队
Posted on: 2005-10-03 17:40

1.不好意思,我国庆出去玩了2天,没有及时回复,我试过,andykid 的方法能达到预定的目的,其中加了static后,其中this.school=school这句可以删掉.
2.//调用父类的构造函数的时候,school还没有初始化成noschool;
//若把school照着andykid所说,加上static限定符,则在调用父类构造函数前,school会被先初始化,try it


bluecrystal ,请教一下,初始化是在this.school=school后完成的吗?如果是,那执行到
super(name,age);时,跳过this.school=schooll,就输出调用子类的方法吗?也就是school显示为null.
也就是我上面第二个问题中,为什么有子类sayHello()
I'm a student,and my name is Liming.My age is 18,and my school is null
I'm a student,and my name is Liming.My age is 18,and my school is beida
的两次输出.
3.wanghcb,我在子类中加了一个sayWelcome()的方法,输出结果为
I am a man,and my name is Liming.My age is 18.
I'm a student,and my name is Liming.My age is 18//像上个问题一样,sayWelcome也有两次输出,为什么呢?
I'm a student,and my name is Liming.My age is 18,and my school is beida

8.Re:在构造方法时虚方法的调用出现错误!--为什么此时的school尚未赋值?? [Re: 科大梦之队] Copy to clipboard
Posted by: bluecrystal
Posted on: 2005-10-03 21:36

如果你使用了static限定符,那么在执行构造函数的时候,先初始化静态成员,然后再执行构造函数里面的语句

9.Re:在构造方法时虚方法的调用出现错误!--为什么此时的school尚未赋值?? [Re: 科大梦之队] Copy to clipboard
Posted by: awardwy
Posted on: 2005-10-03 21:51

哎,school附值在SUPER调用之后啊;

10.Re:在构造方法时虚方法的调用出现错误!--为什么此时的school尚未赋值?? [Re: 科大梦之队] Copy to clipboard
Posted by: awardwy
Posted on: 2005-10-03 22:12

Person s=new Person("Liming",18);
Student p=new Student("Liming",18,"beida");
p.sayHello();//
s.sayHello();//
通过实例对象p,s调用的方法都是各自的实例方法。
在构造器中调用的是被子类覆盖的方法。

11.Re:在构造方法时虚方法的调用出现错误!--为什么此时的school尚未赋值?? [Re: awardwy] Copy to clipboard
Posted by: wanghcb
Posted on: 2005-10-03 23:51

我回答的是“为什么此时的school尚未赋值??”

至于为什么会“有两次输出”,是因为在执行子类构造方法时会现调用超类的构造方法
如果只是达到你说的那个输出,不如按楼上的说法

12.Re:在构造方法时虚方法的调用出现错误!--为什么此时的school尚未赋值?? [Re: 科大梦之队] Copy to clipboard
Posted by: ftang
Posted on: 2005-10-04 05:51

hehe....basic question ah...

make the member variable "static" is not very good idea...I 2 cents is make a static{}, initial ur variable there....

ps question:
which will be init first: static variable or static{}

13.Re:在构造方法时虚方法的调用出现错误!--为什么此时的school尚未赋值?? [Re: wanghcb] Copy to clipboard
Posted by: 科大梦之队
Posted on: 2005-10-04 07:35

wanghcb wrote:
我回答的是“为什么此时的school尚未赋值??”

至于为什么会“有两次输出”,是因为在执行子类构造方法时会现调用超类的构造方法
如果只是达到你说的那个输出,不如按楼上的说法

我明白了,这个"两次输出"问题让我一直感到难以理解,那就是说,执行子类构造方法时,先输出调用的父类构造方法,然后再执行子类的构造方法!

14.Re:在构造方法时虚方法的调用出现错误!--为什么此时的school尚未赋值?? [Re: 科大梦之队] Copy to clipboard
Posted by: lizhihua
Posted on: 2005-10-04 20:47

Person类中的构造方法是两个参数的,而子类Student多了参数Stirng school,子类只调用父类中的两个参数,所以就为null.对吧!!!?
想得到你的的预想的结果,还是按楼上的兄弟分开来调用sayHello()方法。
呵呵,说了这么多废话。

15.Re:在构造方法时虚方法的调用出现错误!--为什么此时的school尚未赋值?? [Re: 科大梦之队] Copy to clipboard
Posted by: wang20051
Posted on: 2005-10-10 12:24


16.Re:在构造方法时虚方法的调用出现错误!--为什么此时的school尚未赋值?? [Re: 科大梦之队] Copy to clipboard
Posted by: q_yuan
Posted on: 2005-10-11 12:03



   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923