日期:2014-05-20 浏览次数:20810 次
class Person {
private String name;
void setName(String n) {
this.name = n;
}
String getName() {
return name;
}
String getInfo() {
return "name is" + this.name;
}
}
class Student extends Person {
private String school;
void setSchool(String s) {
this.school = s;
}
String getSchool() {
return school;
}
String getInfo() {
return "name is " + this.getName() + "; school is " + this.getSchool();
}
}
public class TestPrivate {
public static void main(String[] args) {
Person person = new Person();
Student student = new Student();
student.setName("bill");
student.setSchool("Peking University");
System.out.println(student.getInfo());
}
}
------解决方案--------------------
private私有变量不能被子类直接使用,也可以在Person添加getName方法,然后在子类中直接调用
------解决方案--------------------