父类:
package testone;
public class Pet {
private String name;
private String sex;
private String color;
public Pet() {
// TODO Auto-generated constructor stub
}
public Pet(String name, String sex, String color) {
super();
this.name = name;
this.sex = sex;
this.color = color;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public void show() {
System.out.print(this.getName()+“是一只”+this.getColor()+this.getSex()+“的”);
}
}
子类:
package testone;
public class Dog extends Pet{
private String kind;
public Dog() {
// TODO Auto-generated constructor stub
}
public Dog(String name, String sex, String color, String kind) {
super(name, sex, color);
this.kind = kind;
}
public String getKind() {
return kind;
}
public void setKind(String kind) {
this.kind = kind;
}
public void show() {
super.show();
System.out.println(this.getKind());
}
}
测试类:
package testone;
public class TestDog {
public static void main(String[] args) {
Dog dog = new Dog("翠花","雄性","黄色","菜狗");
dog.show();
}
}
输出结果:
翠花是一只黄色雄性的菜狗