定义一个Person类
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;
}
}
定义一个Student类
public class Student extends Person{
public Student(){}
}
定义一个测试类
public class Mainclass {
public static void main(String[] args) {
// TODO Auto-generated method stub
Student t1=new Student();
t1.setName("zhangsan");
t1.setAge(34);
}
}
通过上面三个类可以看出,Student类虽然只有构造方法,但它从Person类中继承了Person类的方法和属性。 |