A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 彭小康 中级黑马   /  2012-12-11 11:07  /  1438 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

package com.softeem.demo;

/**
*@authorleno
*动物的接口
*/
interface Animal
{
    publicvoid eat(Food food);
}
/**
*@authorleno
*一种动物类:猫
*/
class Cat implements Animal
{
    publicvoid eat(Food food)
    {
      System.out.println("小猫吃"+food.getName());
    }
}
/**
*@authorleno
*一种动物类:狗
*/
class Dog implements Animal
{
    publicvoid eat(Food food)
    {
      System.out.println("小狗啃"+food.getName());
    }
}

/**
*@authorleno
*食物抽象类
*/
abstractclass Food
{
    protected String name;
    public String getName() {
      returnname;
    }

    publicvoid setName(String name) {
      this.name = name;
    }
}

/**
*@authorleno
*一种食物类:鱼
*/
class Fish extends Food
{
    public Fish(String name) {
      this.name = name;
    }
}
/**
*@authorleno
*一种食物类:骨头
*/
class Bone extends Food
{  
    public Bone(String name) {
      this.name = name;
    }
}

/**
*@authorleno
*饲养员类
*
*/
class Feeder
{
    /**
    *饲养员给某种动物喂某种食物
*@paramanimal
    *@paramfood
    */
    publicvoid feed(Animal animal,Food food)
    {
      animal.eat(food);
    }
}

/**
*@authorleno
*测试饲养员给动物喂食物
*/
publicclass TestFeeder {

    publicstaticvoid main(String[] args) {
      Feeder feeder=new Feeder();
      Animal animal=new Dog();
      Food food=new Bone("肉骨头");
feeder.feed(animal,food); //给狗喂肉骨头
animal=new Cat();
      food=new Fish("鱼");
feeder.feed(animal,food); //给猫喂鱼


}
}

评分

参与人数 1技术分 +1 收起 理由
古银平 + 1 神马都是浮云

查看全部评分

1 个回复

正序浏览
您需要登录后才可以回帖 登录 | 加入黑马