使用Java定义一个动物类
属性:name 方法:enjoy() 注:表示宠物开心后的行为
定义一个狗类继承于动物类 包含的属性:furColor 重写enjoy()方法
定义一个猫类继承与动物类 包含的属性:eyesColor 重写enjoy()方法
定义一个女士类 包含的属性:name,所养的宠物 包含的方法:1.构造方法 2.逗宠物开心方法,要求逗宠物开心,宠物执行enjoy()方法进行反应
写一个测试类进行测试,测试一个女士养了一个猫,并逗宠物开心,另一个女士养了一只狗,并逗宠物开心
package t20210902;
public class Demo05 {
public static void main(String[] args) {
Dog dog=new Dog();
dog.name="小黑";
dog.furColor="黑色";
Cat cat=new Cat();
cat.name="小花";
cat.eyesColor="蓝色";
Women women=new Women();
women.name="小王女士";
women.pets=cat.getName();
String a=women.goenjoy("摸了一下");
System.out.print(women.getName()+"养了一只猫,叫"+women.getPets()+","+women.getName()+a+women.getPets()+",");
cat.enjoy();
Women women1=new Women();
women1.name="小李女士";
women1.pets=dog.getName();
String b=women1.goenjoy("踢了一下");
System.out.print(women1.getName()+"养了一只狗,叫"+women1.getPets()+","+women1.getName()+b+women1.getPets()+",");
dog.enjoy();
}
}
class Animal{
String name;
public Animal(){
}
public Animal(String name){
this.name=name;
}
public void enjoy(){
System.out.println("它很开心的跳起来");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
class Dog extends Animal{
String furColor;
public void enjoy(){
System.out.println(name+"很开心,都跳起来了");
}
public String getFurColor() {
return furColor;
}
public void setFurColor(String furColor) {
this.furColor = furColor;
}
}
class Cat extends Animal{
String eyesColor;
public void enjoy(){
System.out.println(name+"很开心,安静的躺着");
}
public String getEyesColor() {
return eyesColor;
}
public void setEyesColor(String eyesColor) {
this.eyesColor = eyesColor;
}
}
class Women{
String name;
String pets;
public Women(){
}
public Women(String name,String pets){
this.name=name;
this.pets=pets;
}
public static String goenjoy(String a){
return a;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPets() {
return pets;
}
public void setPets(String pets) {
this.pets = pets;
}
}
运行结果
|