继承原则:子类只继承超类中非private的成员变量 *隐藏原则(Hiding):子类的成员变量和超类的成员变量同名时,超类的成员变量被隐藏。
若想使用超类的成员变量用super.
class Person{
String name;
int age;
String address;
void setPerson(String name,int age,String address)
{
this.name=name;
this.address=address;
this.age=age;
}
void print()
{
System.out.println("woshiperson");
} }
class Student extends Person{
String department;
void setStudent(String name,int age,String address,String department){ this.name=name;
this.age=age;
this.address=address;
this.department=department;
}
void print(){
System.out.println("woshistudent");
} }
public class Person_ex {
public static void main(String arge[])
{
Person p=new Person();
p.setPerson("tom",21,"121");
Student s=new Student();
s.setStudent("john",22,"336","computer");
/*System.out.println(p.name+" "+p.age+" "+p.address);System.out.println(s.name+" "+s.age+" "+s.address);*/
s.print();
}
} |
|