public class Test
{
public static void main(String[] args)
{
Person person=new Student();
person.age=20;
person.name="Tom";
person.say();
System.out.println(person.country);
}
}
class Person
{
int age;
String name;
String country="China";
public void say()
{
System.out.println("Perosn"+" age:"+age+",name:"+name+" come from:"+country);
}
}
class Student extends Person
{
int scores;
String country="English";
public void say()
{
System.out.println("Student"+" age:"+age+",name:"+name+" come from:"+country);
}
}
打印出的结果是Student age: 20 name:Tom come from English
China
我知道把子类的实例对象给父类引用变量,调用方法的话用的是子类的方法,调用成员变量的画用的是父类的方法,打印出的Student那一行打印的应该是China才对。 |