码迷,mamicode.com
首页 > 其他好文 > 详细

继承的应用案例之宠物商店

时间:2015-05-04 19:50:43      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:

设计一个宠物商店的宠物管理
  1、创建一个宠物类(昵称,价格,种类)并派生各种宠物,每种宠物都有各自吃食的方法
  2、创建一个商店类,可以根据种类显示该种类所有的宠物信息

import java.util.Arrays;

public class HomeWork1 {
    public static void main(String[] args) {
        Dog d1 = new Dog("小白",2000.0f, "狗");
        Dog d2 = new Dog("小黑",1000.0f, "狗");
        Dog d3 = new Dog("小黄",800.0f, "狗");
        
        Cat c1 = new Cat("汤姆",200.0f, "猫");
        Cat c2 = new Cat("咪咪",400.0f, "猫");
        Cat c3 = new Cat("笨笨",600.0f, "猫");
        
        PetShop ps = new PetShop();
        ps.add(d1);
        ps.add(d2);
        ps.add(d3);
        ps.add(c1);
        ps.add(c2);
        ps.add(c3);
        
        for(Pet p:ps.getPets()){
            System.out.println(p.getInfo());
        }
        System.out.println("-----------------------");
        Pet[] pets = ps.findByType("狗");
        for(Pet p:pets){
            System.out.println(p.getInfo());
            p.eat();
        }
    }
}


//宠物类
class Pet{
    private String name;
    private float price;
    private String type;
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public float getPrice() {
        return price;
    }
    public void setPrice(float price) {
        this.price = price;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    
    public Pet(String name, float price, String type) {
        super();
        this.name = name;
        this.price = price;
        this.type = type;
    }
    
    //宠物吃东西的方法
    public void eat(){
        System.out.println(type+"吃饭了");
    }
    
    //宠物信息
    public String  getInfo(){
        return "我是一只"+type+",小伙伴们都叫我"+name+",喜欢我就把我带走吧,只需RMB+price";
    }
}

//狗类
class Dog extends Pet{
    
    public Dog(String name, float price, String type) {
        super(name, price, type);
    }

    //复写吃食的方法
    public void eat(){
        System.out.println("我喜欢啃大棒骨");
    }
}


//猫类
class Cat extends Pet{
    
    public Cat(String name, float price, String type) {
        super(name, price, type);
    }

    //复写吃食的方法
    public void eat(){
        System.out.println("我喜欢吃鱼");
    }
}

//宠物商店
class PetShop{
    private Pet[] pets = new Pet[3];
    private int count;//计数器
    
    //添加
    public void add(Pet pet){
        if(count>=pets.length){
            int newLen = (pets.length*3)/2+1;
            pets = Arrays.copyOf(pets, newLen);
        }
        pets[count] = pet;
        count++;
    }
    
    //获取所有宠物对象
    public Pet[] getPets(){
        Pet[] ps = new Pet[count];
        for(int i = 0;i<count;i++){
            ps[i] = pets[i];
        }
        return ps;
    }
    
    //根据宠物的种类,查找宠物的信息
    public Pet[] findByType(String type){
        PetShop ps = new PetShop();
        
        for(int i = 0; i<count;i++){
            if(pets[i].getType().equals(type)){//这里哪个比较确定就在前面调用equals方法,
                ps.add(pets[i]);
            }
        }
        return ps.getPets();
    }
}

 

继承的应用案例之宠物商店

标签:

原文地址:http://www.cnblogs.com/LO-ME/p/4476876.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!