2017.12.13T19_B2_6zuoye

package com.whxiong.work06;
/*
 * 动物类接口
 */
public interface Animal {
 /**
  * 动物的叫声
  */
 void shout();
}
 /**
  * 狗狗类
  * @author whx
  */
 class Dog implements Animal{
  /**
   * 狗狗的叫声
   */
  @Override
  public void shout() {
   System.out.println("汪...汪...汪...");
  }
 }
 /**
  * 小猫类
  * @author whx
  */
 class Cat implements Animal{
  /**
   * 小猫叫声
   */
  @Override
  public void shout() {
   System.out.println("喵...喵...喵...");
  }
 }
 /**
  * 小猪类
  * @author whx
  */
 class Pig implements Animal{
  /**
   * 小猪叫声
   */
  public void shout(){
   System.out.println("哼...哼...哼...");
  }
 }
 class Store{
  /**
   * 创建静态的get方法
   * @param choice
   * @return
   */
  public static Animal get(int choice){
   Animal al=null;
   if(choice==1){
    al=new Dog();
    al.shout();
   }else if(choice==2){
    al=new Cat();
    al.shout();
   }else if(choice==3) {
    al=new Pig();
    al.shout();
   }else{
    System.out.println("没有该类型动物!");
   }
   return al;
  }
 }

*****************************************************

package com.whxiong.work06;

import java.util.Scanner;  /**   * 第六章课后习题  测试类   * @author whx   */ public class Demo {

 /**   * @param args   */  public static void main(String[] args) {   Scanner input =new Scanner(System.in);   System.out.println("课后习题第四题");   System.out.println("*******************************");   /**    * 选择动物类型    */   System.out.println("请选择动物类型:1.狗狗 2.小猫 3.小猪");    int choice =input.nextInt();    /**     * 调用Store中的get方法     * 因为是静态方法,所以直接用》》类名.方法名();     */    Animal al=Store.get(choice);         System.out.println();   System.out.println("课后习题第五题");   System.out.println("*******************************");   /**    * 选择宠物类型    */   System.out.println("请选择宠物类型:1.狗狗 2.企鹅");   int choiceType=input.nextInt();   /**    * 实例化主人类    * 并且调用print方法    */   Master mt= new Master("魏先生");   mt.print(choiceType);

****************************

package com.whxiong.work06;
/**
 * 吃饭功能 接口
 * @author whx
 */
public interface Eatable {
 void eat();
}
 /**
  * 接飞盘功能 接口
  * @author whx
  */
  interface FlyingDiscCatchable{
  void catchingFlyDisc();
 }
  /**
   * 游泳功能 接口
   * @author whx
   */
  interface Swimmable{
   void swim();
  }

*****************************************

 class Penguin extends Pet implements Eatable,Swimmable{
  /**
   * 调用父类带参构造
   * @param name
   * @param health
   * @param love
   */
  public Penguin(String name) {
   super(name);
  }
  /**
   * 游泳
   */
  @Override
  public void swim() {
   System.out.println("企鹅"+super.getName()+"正在欢快的游泳!");
  }
  /**
   * 吃东西
   */
  @Override
  public void eat() {
   System.out.println("企鹅"+super.getName()+"正在开心的吃小鱼虾!");
  }
  /**
   * 重写print方法
   */
  @Override
  public void print() {
   super.health=super.health-3;
   super.love=super.love+5;
  System.out.println("企鹅"+super.getName()+"玩的好开心 它的健康值是:"+super.health
    +" 它和主人的亲密度为:"+super.love+"。");
  }
  
 }
 /**
  * 主人类
  * @author whx
  */
 class Master{
  /**
   * 主人姓名
   */
  private String name;
  /**
   * 创建带参构造
   * @param name
   */
  public Master(String name){
   this.name=name;
  }
  /**
   * 选择宠物类型,输出宠物信息
   * @param choiceType
   * @return
   */
  public  Pet print(int choiceType){
   Scanner input =new Scanner(System.in);
   Pet pet=null;
   if(choiceType==1){
    System.out.println("输入狗狗的宠物名:");
    String name=input.next();
    /**
     * 实例化狗狗类
     */
    pet=new Dog1(name);
    /**
     * 调用狗狗类中的方法
     * 因为向下转型,强制转型
     */
    ((Dog1) pet).catchingFlyDisc();
    ((Dog1) pet).eat();
    pet.print();
   }
   else if(choiceType==2){
    System.out.println("输入企鹅的宠物名:");
    String name=input.next();
    /**
     * 实例化企鹅类
     */
    pet=new Penguin(name);
    /**
     * 调用企鹅类中的方法
     * 因为向下转型,强制转型
     */
    ((Penguin) pet).swim();
    ((Penguin) pet).eat();
    pet.print();
   }else{
    System.out.println("主人还没领养该类型宠物!");
    /**
     * 直接退出
     */
    System.exit(0);
   }
   System.out.println("谢谢"+this.name+"!");
   return pet;
  }
  
   
 }

******************************************


 

原文地址:https://www.cnblogs.com/xiaoxiao1016/p/8031241.html