父类
/**
* 父类
*/
public class Fu {
/**姓名*/
private String name;
/**年龄*/
private int age;
public Fu(){
}
public Fu(String name, int age){
this.name = name;
this.age = age;
}
/**
* 获取姓名
*/
public String getName() {
return name;
}
/**
* 设置姓名
* @param name 姓名
*/
public void setName(String name) {
this.name = name;
}
/**
* 获取年龄
*/
public int getAge() {
return age;
}
/**
* 设置年龄
*/
public void setAge(int age) {
this.age = age;
}
}
子类 /**
*子类
*/
public class Zi extends Fu {
public Zi() {
super();//提醒你你不写的他会自动提供
}
public Zi(String name, int age){
//调用父类的构造方法,初始化父类的属性
super(name, age);
}
}
测试 /**
*测试类
*/
public class Test {
public static void main(String[] args) {
Zi zi1 = new Zi();
Zi zi2 = new Zi("张三",12);
System.out.println(zi1.getName());
System.out.println(zi2.getName());
zi1.setName("李四");
System.out.println(zi1.getName());
}
}
|
|