面向对象程序设计——第六章接口课后作业

1.在第三题的进行功能扩展。

package come.job.dome;
/**
 * 
 * @author 宠物类
 *
 */
public class Store {
	public static Animal get(int choice) {
		if(choice==1) { //狗狗
			return new Dog();
		}else if(choice==2) { //猫
			return new Cat();
		}else {
			return new Pig(); //猪
		}
	}
}
package come.job.dome;
/**
 * 
 * @author 狗狗
 *
 */
public class Dog implements Animal {
	public void show() {
		System.out.println("W W!");
	}
}
package come.job.dome;
/**
 * 
 * @author 猫
 *
 */
public class Cat implements Animal {
	public void show() {
		System.out.println("M M!");
	}
}
package come.job.dome;
/**
 * 
 * @author 猪
 *
 */
public class Pig implements Animal{
	public void show() {
		System.out.println("猪叫");
	}
}
package come.job.dome;
/**
 * 
 * @author 接口
 *
 */
public interface Animal {
	void show();
}
package come.job.dome;

import java.util.Scanner;
/**
 * 
 * @author 测试类
 *
 */
public class AnimalTest {
	public static void main(String[] args) {
		System.out.print("请输入要选择宠物的编号(1.狗,2.猫,3.猪):");
		Scanner input = new Scanner(System.in);
		int choice = input.nextInt();
		Animal store = Store.get(choice);
		if(choice==1) {
			store.show();
		}else if(choice==2) {
			store.show();
		}else {
			store.show();
		}
	}
}

2..使用面向接口思想对电子宠物系统类结构进行重构

package bdqn.com;
/**
 * 
 * @author 宠物类
 *
 */
public abstract class Pet {
	protected String name = "无名氏";
	protected int health =100;
	protected int love = 0;
	public Pet(String name) {
		this.name = name;
	}
	/**
	 * 输出宠物信息
	 */
	public void print() {
		System.out.println("我的名字叫"+this.name+",健康值是"+this.health+",和主人的亲密度是"+this.love);
	}
}
package bdqn.com;
/**
 * 
 * @author 狗狗类
 *
 */
public class Dog extends Pet implements Eatable,Flying{
	private String strain; //品种
	public Dog(String name,String strain) {
		super(name);
		this.strain = strain;
	}
	public void print() {
		super.print();
		System.out.println("我是一只"+this.strain);
	}
	@Override
	public void flyDis() {
		// TODO Auto-generated method stub
		super.health = super.health-10;
		System.out.println("和狗狗玩接飞盘的游戏!狗狗健康值减10");
	}
	@Override
	public void eat() {
		// TODO Auto-generated method stub
		super.health = super.health+3;
		System.out.println("喂狗狗吃狗粮!狗狗健康值加3");
	}
	
}
package bdqn.com;
/**
 * 
 * @author 企鹅类
 *
 */
public class Penguin extends Pet implements Eatable,Swimmable{
	private String sex;
	public Penguin(String name,String sex) {
		super(name);
		this.sex = sex;
	}
	public void print() {
		super.print();
		System.out.println("我的性别是:"+this.sex);
	}
	
	@Override
	public void swin() {
		// TODO Auto-generated method stub
		super.health = super.health-10;
		System.out.println("和企鹅玩游泳游戏!企鹅健康值减10");
	}
	@Override
	public void eat() {
		// TODO Auto-generated method stub
		super.health = super.health+3;
		System.out.println("喂企鹅吃鱼!企鹅健康值加3");
	}
	
}
package bdqn.com;
/**
 * 吃饭的接口
 * @author 接口
 *
 */
public interface Eatable {
	void eat(); //吃饭功能
}
package bdqn.com;
/**
 * 接飞盘接口
 * @author 接口
 *
 */
public interface Flying {
	void flyDis(); //接飞盘
}
package bdqn.com;
/**
 * 游泳的接口
 * @author 接口
 *
 */
public interface Swimmable {
	void swin(); //游泳
}
package bdqn.com;
/**
 * 
 * @author 测试类
 *
 */
public class TestFeed {
	public static void main(String[] args) {
		Dog pet = new Dog("黑宝","拉布拉多犬");
		pet.print();
		pet.eat();
		pet.flyDis();
		System.out.println();
		Penguin pet1 = new Penguin("楠楠","Q妹");
		pet1.print();
		pet1.eat();
		pet1.swin();
		
	}
}


猜你喜欢

转载自blog.csdn.net/qq_41882685/article/details/80184801