1.在第三题的进行功能扩展。
(1)增加一种新的动物类型:Pig(猪),实现shout()方法。
(2)修改Store类的get()方法:如果传入的参数是字符串dog,则返回一个Dog对象;如果传入的参数是字串pig,则返回一个Pig对象;否则,返回一个Cat对象。
(3)在测试类Test中加以测试:向Store类的get()方法中传入参数“pig”,并返回的对象中调用shout()方法,看看与预期的结果是否一致。
(1)增加一种新的动物类型:Pig(猪),实现shout()方法。
(2)修改Store类的get()方法:如果传入的参数是字符串dog,则返回一个Dog对象;如果传入的参数是字串pig,则返回一个Pig对象;否则,返回一个Cat对象。
(3)在测试类Test中加以测试:向Store类的get()方法中传入参数“pig”,并返回的对象中调用shout()方法,看看与预期的结果是否一致。
public interface Animal { void shout(); } class Dog implements Animal { public void shout() { System.out.println("W W!"); } } class Pig implements Animal { public void shout() { System.out.println("L L!"); } } class Cat implements Animal { public void shout() { System.out.println("M M!"); } } class Store { public static Animal get(String choice) { if (choice.equalsIgnoreCase("dog")) { return new Dog(); } else if (choice.equalsIgnoreCase("Pig")) { return new Pig(); } else { return new Cat(); } } } public class AnimalTest { public static void main(String[] args) { Animal al = Store.get("pig"); al.shout(); } }
2.对贯穿本书的案列电子宠物系统的类结构进行重构,要求如下:
>定义Eatble接口,在接口中定义eat( )方法,表示吃饭功能。
>定义FlyingDiscCatchable接口,在接口定义中catchingFiyDise( )方法,表示接飞盘功能。
>定义Swimmable接口,在接口中定义swim( )方法,表示游泳功能。
>定义抽象类Pet,包括宠物名称(mane),健康值(health),和主人亲密度(love)属性。并提供抽象方法print( ),用来输出宠物信息。
>定义狗类Dog,继承Pet类,实现Eatable和FlyingDiscCatchable接口,并重写或实现各个方法。
>定义企鹅类Penguin,继承Pet类,实现Eatable和Swimmble接口,并重写或实现各个方法。
>编写测试类,实现狗吃饭,企鹅游泳和狗玩飞盘游戏的功能,并输出企鹅信息。
public interface Eatable { /** * 吃方法 */ void eat(); } public interface FlyingDiscCatchable { /** * 接飞盘功能 */ void catchiFlyDisc(); } public interface Swimmable { /** * 表示游泳功能 */ void swim(); } public abstract class Pet { String name; //名称 String health;//健康值 String love; //亲密度 public Pet(String name, String health, String love) { this.name = name; this.health = health; this.love = love; } /** * 抽象方法 */ public abstract void print(); } public class Dog1 extends Pet implements Eatable, FlyingDiscCatchable { public Dog1(String name, String health, String love) { super(name, health, love); // TODO Auto-generated constructor stub } @Override public void catchiFlyDisc() { // TODO Auto-generated method stub System.out.println("狗狗"+super.name+"正在接飞盘。"); super.health = super.health+10; super.love = super.love+5; } @Override public void eat() { // TODO Auto-generated method stub super.health = super.health+4; System.out.println("狗狗"+super.name+"吃饱了!健康值增加4。"); } @Override public void print() { // TODO Auto-generated method stub System.out.println("宠物自白:\n我的名字叫:"+this.name+",健康值是:"+this.health+",亲密度:"+this.love); } } public class Penguin extends Pet implements Eatable, Swimmable { public Penguin(String name, String health, String love) { super(name, health, love); // TODO Auto-generated constructor stub } @Override public void swim() { //游泳 // TODO Auto-generated method stub System.out.println("企鹅"+super.name+"正在游泳。"); super.health = super.health+15; super.love = super.love+5; } @Override public void eat() { //吃的方法 // TODO Auto-generated method stub super.health = super.health+5; System.out.println("企鹅"+super.name+"吃饱了!健康值增加5。"); } @Override public void print() { //输出打印 // TODO Auto-generated method stub System.out.println("宠物自白:\n我的名字叫:"+this.name+",健康值是:"+this.health+",亲密度:"+this.love); } } public class TestPet { public static void main(String[] args) { Dog1 dog = new Dog1("阿狗","60","60"); dog.print(); dog.catchiFlyDisc(); dog.eat(); Penguin pgn = new Penguin("拉卡","85","80"); pgn.print(); pgn.swim(); pgn.eat(); } }