第六章:接口

第一题:在第三题的基础上进行功能扩展,要求如下:

增加一种新的动物类型:pig(猪),实现shout()方法

修改Store类的get()方法:如果传入的参数是字符串dog,则返回 一个Dog对象;如果传入的参数是字符串pig则返回一个pig对象;否则,返回一个Cat对象

在测试类Test中加以测试:向Store类的个get()方法中传入参数“pig”,并在返回的对象中调用shout()方法,看看于预期的结果是否一致。

package cn.bdqn.bemo99;


public interface Animal {
void shout();


}

package cn.bdqn.bemo99;


public class Dog implements Animal {


public void shout() {
System.out.println("W W!");


}


}

package cn.bdqn.bemo99;


public class Cat implements Animal {


@Override
public void shout() {
System.out.println("M M!");
}


}

package cn.bdqn.bemo99;


public class Pig implements Animal {


@Override
public void shout() {
System.out.println("O O!");


}


}

package cn.bdqn.bemo99;


import java.security.CryptoPrimitive;


public class Store {
public static Animal shout(String choice) {
if (choice.equalsIgnoreCase("dog")) {
return new Dog();
} else {if (choice.equalsIgnoreCase("pig")) {
return new Pig();
}else
return new Cat();



}

}

}

package cn.bdqn.bemo99;


public class AnimalTest {
public static void main(String[] args) {
Animal a = Store.shout("dog");
a.shout();
Animal a1=Store.shout("cat");
a1.shout();
Animal a2=Store.shout("pig");
a2.shout();

}

第二题:对贯穿本书的案例电子宠物系统的类结构进行重构,

package cn.bdqn.bemo999;


public interface Eatable {
void eat(); //吃饭功能


}

package cn.bdqn.bemo999;


public interface FlyingDiscCatchable {
void catchingFlyDisc(); //接飞盘功能


}

package cn.bdqn.bemo999;


public interface Swimmable {
void swim(); //游泳功能


}

package cn.bdqn.bemo999;


public abstract class Pet {
private String name;//名字
private int health;//健康值
private int love;//亲密度
public String getName(){
return name;


}
public int getHealth() {
return health;
}
public void setHealth(int health) {
this.health = health;
}
public int getLove() {
return love;
}
public void setLove(int love) {
this.love = love;
}
public void setName(String name) {
this.name = name;
}
public abstract void show();


}

package cn.bdqn.bemo999;


public class Dog extends Pet implements Eatable, FlyingDiscCatchable {


@Override
public void catchingFlyDisc() {
System.out.println("狗狗"+this.getName()+"正在玩接飞盘!");


}


@Override
public void eat() {
System.out.println("狗狗"+this.getName()+"吃饱了!");


}


@Override
public void show() {
System.out.println("宠物自白:\n我的名字叫:"+this.getName()+"\n我的健康值是:"+this.getHealth()+"\n和主人的亲密度为:"+this.getLove());
eat();
catchingFlyDisc();

}


}

package cn.bdqn.bemo999;


public class Penguin extends Pet implements Eatable, Swimmable {


@Override
public void swim() {
System.out.println("企鹅"+this.getName()+"正在游泳");
}


@Override
public void eat() {
System.out.println("企鹅"+this.getName()+"吃饱了");


}


@Override
public void show() {
System.out.println("宠物的自白:\n我的名字叫:"+this.getName()+"\n我的健康值为:"+this.getHealth()+"和主人的亲密度"+this.getLove());
eat();
swim();


}


}

package cn.bdqn.bemo999;


public class Test {
public static void main(String[] args) {
Pet pet=new Dog();
pet.setName("泰迪");//赋值
pet.setHealth(100);
pet.setLove(50);
pet.show();//输出信息
Pet pet1=new Penguin();
pet1.setName("小企鹅");//赋值
pet1.setHealth(100);
pet1.setLove(60);
pet1.show();//输出信息
}


}

猜你喜欢

转载自blog.csdn.net/liyanghahahhaha/article/details/80517320