package chapter4;
class Person{
String name;
int age;
public void getInfo(){
System.out.println(name);
System.out.println(age);
}
}public class Student{ public static void main(String[] args) {
Person p=new Person();
p.name="person";
p.age=30;
p.getInfo();
Student1 s=new Student1();
s.name="student1";
s.age=13;
s.school="school";
s.getInfo();
s.Study();
}}class Student1 extends Person{
String school=new String();
public void Study(){
System.out.println("Studding");
}
public void getInfo(String name,int age,String school){
super.getInfo();
System.out.println(school);
}
}
最后结果怎么不输出school
而且 Student1里的getInfo:
public void getInfo(String school){}
就可以了。
class Person {
String name; int age; public void getInfo() {
System.out.println(name);
System.out.println(age);
}
}public class Student { public static void main(String[] args) {
Person p = new Person();
p.name = "person";
p.age = 30;
p.getInfo(); Student1 s = new Student1();
s.name = "student1";
s.age = 13;
s.getInfo("school");
s.Study();
}}class Student1 extends Person {
String school = new String(); public void Study() {
System.out.println("Studding");
} public void getInfo(String school) {
super.getInfo();
System.out.println(school);
}
}
Person p=new Person();
p.name="person";
p.age=30;
p.getInfo();
Student1 s=new Student1();
s.name="student1";
s.age=13;
s.school="school";
s.getInfo();
s.Study();
}
}
你main里的s.getInfo();调用的是Person类中的getInfo(),而不是Student1类中的。
所以如果想输出school,你需要把main函数中的s.getInfo()传进值,
改为:s.getInfo("student1",13,"school"); 这样是调用Student1类中的方法了
看看继承方面的知识,可能是你不小心马虎了呵呵。加油~
super.getInfo();
System.out.println(school);
这样就不能输出school,可把参数school删了,就能输出了请问怎么回事?