Pet类package com.aaa.adv;/** * 父类:宠物类 * @author 张晨光 * */public class Pet {//成员变量private String name;private int health;//public Pet(){}//构造public Pet(String name, int health) {super();this.name = name;this.health = health;}//生成getter/setter;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getHealth() {return health;}public void setHealth(int health) {this.health = health;}//增加一个方法:eatpublic void eat(){System.out.println("宠物类开始虚拟的吃");}//针对公共的方法,一定要学会提取出来public void eat(String food){System.out.println(this.getName()+"吃"+food);}}
Cat类package com.aaa.adv;public class Cat extends Pet {public Cat(String name, int health) {super(name, health);}}
Dog类package com.aaa.adv;public class Dog extends Pet {public Dog(String name, int health) {super(name, health);// TODO Auto-generated constructor stub}//Dog子类,本身仍然保留了父类的eat()不带参