//这就是一个封装类,封装的思想就是属性私有化,提供可以访问设置属性的共有方法
public class Person {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void talk(){
System.out.println("一个人在说话");
}
}
//学生继承了人类,因为学生也是人(废话)
public class Student extends Person {
//班级
private String theClass;
//地址
private String address;
public String getTheClass() {
return theClass;
}
public void setTheClass(String theClass) {
this.theClass = theClass;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public void talk() {
System.out.println("一个学生在说话");
}
public static void main(String[] args){
//多态也就是父类引用指向子类对象
Person student = new Student();
student.talk();
}
} |