package com.bdqn.thesixthchapter1;
/**
* 第6章接口 第4题在第3题的基础上进行功能扩展,要求如下。 增加一种新的动物类型:Pig(猪),实现shout()方法。
* 修改Store类的get()方法:如果传入的参数是字符串dog,则放回一个Dog对象;如果传入的参数是字符串pig,则返回一个Pig对象
* 否则,返回一个Cat对象。
* 在测试类Test中加以测试:向Store类的get()方法中传入参数"pig",并在返回的对象中调用shout()方法,看看与预期的结果是否一致。
*
*/
public class Store {
public static Animal get(String animal) {
if (animal.equalsIgnoreCase("dog")) {
return new Dog();
} else if (animal.equalsIgnoreCase("pig")) {
return new Pig();
} else {
return new Cat();
}
}
}
* 第6章接口 第4题在第3题的基础上进行功能扩展,要求如下。 增加一种新的动物类型:Pig(猪),实现shout()方法。
* 修改Store类的get()方法:如果传入的参数是字符串dog,则放回一个Dog对象;如果传入的参数是字符串pig,则返回一个Pig对象
* 否则,返回一个Cat对象。
* 在测试类Test中加以测试:向Store类的get()方法中传入参数"pig",并在返回的对象中调用shout()方法,看看与预期的结果是否一致。
*
*/
public class Store {
public static Animal get(String animal) {
if (animal.equalsIgnoreCase("dog")) {
return new Dog();
} else if (animal.equalsIgnoreCase("pig")) {
return new Pig();
} else {
return new Cat();
}
}
}
package com.bdqn.thesixthchapter1;
/**
* @author 123123
*
*/
public interface Animal {
void shout();
* @author 123123
*
*/
public interface Animal {
void shout();
}
package com.bdqn.thesixthchapter1;
/**
*
* @author 123123
*狗类
*/
public class Dog implements Animal {
/**
*
* @author 123123
*狗类
*/
public class Dog implements Animal {
@Override
public void shout() {
System.out.println("汪汪");
}
public void shout() {
System.out.println("汪汪");
}
}
package com.bdqn.thesixthchapter1;
/**
*
* @author 123123
*猫类
*/
public class Cat implements Animal{
/**
*
* @author 123123
*猫类
*/
public class Cat implements Animal{
@Override
public void shout() {
System.out.println("喵喵");
}
public void shout() {
System.out.println("喵喵");
}
}
package com.bdqn.thesixthchapter1;
/**
*
* @author 123123
* 猪类
*/
public class Pig implements Animal{
/**
*
* @author 123123
* 猪类
*/
public class Pig implements Animal{
@Override
public void shout() {
System.out.println("哼哼");
}
public void shout() {
System.out.println("哼哼");
}
}
package com.bdqn.thesixthchapter1;
public class Test {
public static void main(String[] args) {
Animal animal = Store.get("pig");
animal.shout();
}
Animal animal = Store.get("pig");
animal.shout();
}
}
package com.bdqn.thesixthchapter;
/**
* 第6题接口
* 第5题对贯穿本书的案例电子宠物系统的类结构进行重构,要求如下。
* 抽象类
*/
public abstract class Pet {
private String name; //名称
private int health; //健康值
private int love; //新密度
public Pet(String name,int health,int love) {
this.name=name;
this.health=health;
this.love=love;
}
public abstract void print();
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;
}
public int getLove() {
return love;
}
public void setLove(int love) {
this.love = love;
}
/**
* 第6题接口
* 第5题对贯穿本书的案例电子宠物系统的类结构进行重构,要求如下。
* 抽象类
*/
public abstract class Pet {
private String name; //名称
private int health; //健康值
private int love; //新密度
public Pet(String name,int health,int love) {
this.name=name;
this.health=health;
this.love=love;
}
public abstract void print();
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;
}
public int getLove() {
return love;
}
public void setLove(int love) {
this.love = love;
}
public void eat() {
// TODO Auto-generated method stub
}
// TODO Auto-generated method stub
}
}
/**
*
*/
package com.bdqn.thesixthchapter;
*
*/
package com.bdqn.thesixthchapter;
/**
* @author 123123
* 吃饭功能
*/
public interface Eatable {
void eat(String food);
}
* @author 123123
* 吃饭功能
*/
public interface Eatable {
void eat(String food);
}
/**
*
*/
package com.bdqn.thesixthchapter;
*
*/
package com.bdqn.thesixthchapter;
/**
* @author 123123
* 接飞盘功能
*/
public interface FlyingDiscCatchable {
void catchingFlyDisc();
}
* @author 123123
* 接飞盘功能
*/
public interface FlyingDiscCatchable {
void catchingFlyDisc();
}
/**
*
*/
package com.bdqn.thesixthchapter;
*
*/
package com.bdqn.thesixthchapter;
/**
* @author 123123
* 游泳功能
*/
public interface Swimmable {
void swim();
* @author 123123
* 游泳功能
*/
public interface Swimmable {
void swim();
}
package com.bdqn.thesixthchapter;
/**
*
* @author 123123
* 狗类
*/
public class Dog extends Pet implements Eatable,FlyingDiscCatchable {
/**
*
* @author 123123
* 狗类
*/
public class Dog extends Pet implements Eatable,FlyingDiscCatchable {
public Dog(String name, int health, int love) {
super(name, health, love);
// TODO Auto-generated constructor stub
}
super(name, health, love);
// TODO Auto-generated constructor stub
}
@Override
public void print() {
System.out.println("宠物名称:"+getName()+"\t健康值:"+getHealth()+"\t亲密度:"+getLove());
}
public void print() {
System.out.println("宠物名称:"+getName()+"\t健康值:"+getHealth()+"\t亲密度:"+getLove());
}
@Override
public void catchingFlyDisc() {
System.out.println("狗玩接飞盘游戏");
}
public void catchingFlyDisc() {
System.out.println("狗玩接飞盘游戏");
}
@Override
public void eat(String food) {
System.out.println("狗正在吃"+food);
}
public void eat(String food) {
System.out.println("狗正在吃"+food);
}
}
/**
*
*/
package com.bdqn.thesixthchapter;
*
*/
package com.bdqn.thesixthchapter;
/**
* @author 123123
* 企鹅类
*/
public class Penguin extends Pet implements Eatable,Swimmable {
* @author 123123
* 企鹅类
*/
public class Penguin extends Pet implements Eatable,Swimmable {
public Penguin(String name, int health, int love) {
super(name, health, love);
// TODO Auto-generated constructor stub
}
super(name, health, love);
// TODO Auto-generated constructor stub
}
@Override
public void swim() {
System.out.println("企鹅正在游泳");
}
public void swim() {
System.out.println("企鹅正在游泳");
}
@Override
public void eat(String food) {
System.out.println("企鹅正在吃"+food);
}
public void eat(String food) {
System.out.println("企鹅正在吃"+food);
}
@Override
public void print() {
System.out.println("宠物名称:"+getName()+"\t健康值:"+getHealth()+"\t亲密度:"+getLove());
}
public void print() {
System.out.println("宠物名称:"+getName()+"\t健康值:"+getHealth()+"\t亲密度:"+getLove());
}
}
package com.bdqn.thesixthchapter;
/**
*
* @author 123123
* 测试
*/
public class Test {
/**
*
* @author 123123
* 测试
*/
public class Test {
public static void main(String[] args) {
Dog dog = new Dog("哈巴狗", 100, 59);
dog.print();
dog.catchingFlyDisc();
dog.eat("狗粮");
System.out.println("");
Penguin pu = new Penguin("企企", 100, 60);
pu.print();
pu.eat("鱼");
pu.swim();
Dog dog = new Dog("哈巴狗", 100, 59);
dog.print();
dog.catchingFlyDisc();
dog.eat("狗粮");
System.out.println("");
Penguin pu = new Penguin("企企", 100, 60);
pu.print();
pu.eat("鱼");
pu.swim();
}
}