子类继承的方法,为什么输出的却是父类的成员变量?
下面这个程序中,输出的结果是:
my name is wangwu and my age is 10
john
21
我的想法是如果要是子类继承了父类的shout()方法,那么输出的应该是子类的name和age的值啊,怎么会是这样的输出结果???
class Person
{
String name = "wangwu ";
int age = 10;
public Person()
{
}
public Person(String name, int age)
{
this.name = name;
this.age = age;
}
void shout()
{
System.out.println( "my name is " + name + " and my age is " + age);
}
}
public class Student extends Person
{
String major;
String name = "lisi ";
int age = 12;
public Student()
{
super();
}
public Student(String name,int age)
{
//super(name,age);
this.name = name;
this.age = age;
}
public static void main(String args [])
{
Student s1 = new Student( "john ",21);
s1.shout();
System.out.println(s1.name);
System.out.println(s1.age);
}
}
如果将下面程序段:
//super(name,age);
this.name = name;
this.age = age;
改为:
super(name,age);
//this.name = name;
//this.age = age;
程序的输出结果是:
my name is lisi and my age is 21
lisi
12
给我的感觉好像是shout()方法中始终使用的是父类的成员变量name和age,而子类已经继承了父类,为什么在shout()方法中使用的不是父类的成员变量呢?
请明示!!在内存中的状态又是什么样的呢?
------解决方案--------------------第一种情况:Student s1 = new Student( "john ",21);将参数john,21传给构造方法public Student(String name,int age)
{
//super(name,age);
this.name = name;
this.age = age;
}在此构造方法中将参数符值给了name and age 故打印的是john,21,因为student 继承person 并没有重写shot()方法,故s1.shout();还是引用父类的方法故会输出my name is wangwu and my age is 10
第二种情况:Student s1 = new Student( "john ",21);将参数john,21传给构造方法public Student(String name,int age)
{
super(name,age);
//this.name = name;
//this.age = age;
}此构造方法super父类的方法,即public Person(String name, int age)
{
this.name = name;
this.age = age;
}这个时候就把子类里定义的name and age 值传了过去,所以输出的是lisi 12
同样,由于此时子类super父类的方法,并将值john,21传给了父类的name and age ,故shout()显示结果为:my name is john and my age is 21
------解决方案--------------------学习了!!!!
解释得真好!!!
------解决方案--------------------留个名字
------解决方案--------------------老大,很简单的问题弄这么多,你没有重载超类里的void shout()方法
超类里打印的是超类的实例变量,你把超类里的这个方法重载一下!