用链表实现宠物商店的定义与简单操作

import com.demo.LinkDemo.*; //导入链表类包
//定义宠物标准
@SuppressWarnings("unused")
interface Pet {
    public String getName() ; //获得名字
    public String getColor() ; //获得颜色
}
//宠物商店
class PetShop {
    private ILink<Pet> allPets = new LinkImpl<Pet>() ; //(存在对象向上转型模式)保存多个宠物信息的链表类
    //追加宠物
    public void add(Pet pet) {
        this.allPets.add(pet) ;
    }
    //删除宠物
    public void delete(Pet pet) {
        this.allPets.remove(pet) ;
    }
    //关键字查询
    public ILink<Pet> search(String keyword) {
        Object result[] = this.allPets.toArray() ; //获取全部数据
        ILink<Pet> searchResult = new LinkImpl<Pet>() ; //将查询到的数据保存到链表中
        if(result != null) {
            /*
            foreach的语句(增强for循环)格式:
            for(元素类型t 元素变量x : 遍历对象obj){
                 引用了x的java语句;
            }
            */
            for(Object obj : result) {
                Pet pet = (Pet) obj ; //向下转型
                if(pet.getName().contains(keyword) || pet.getColor().contains(keyword)) {
                    searchResult.add(pet) ; //保存查询结果
                }
            }
        }
        return searchResult ;
    }
}
//让猫实现宠物标准
class Cat implements Pet {
    private String name ;
    private String color ;
    public Cat(String name, String color) {
        this.name = name ;
        this.color = color ;
    }
    public String getName() {
        return this.name;
    }
    public String getColor() {
        return this.color ;
    }
    //
    public boolean equals(Object obj) {
        if(obj == null) {
            return false;
        }
        if(!(obj instanceof Cat)) {
            return false ;
        }
        if(this == obj) {
            return true ;
        }
        Cat cat = (Cat) obj ;
        return this.name.equals(cat.name) && this.color.equals(cat.color);
    }

    public String toString() {
        return "【宠物猫】名称:" + this.name + ";颜色:" + this.color;
    }
}
//让狗实现宠物标准
class Dog implements Pet {
    private String name ;
    private String color ;
    public Dog(String name, String color) {
        this.name = name ;
        this.color = color ;
    }
    public String getName() {
        return this.name;
    }
    public String getColor() {
        return this.color ;
    }
    //
    public boolean equals(Object obj) {
        if(obj == null) {
            return false;
        }
        if(!(obj instanceof Dog)) {
            return false ;
        }
        if(this == obj) {
            return true ;
        }
        Dog dog = (Dog) obj ;
        return this.name.equals(dog.name) && this.color.equals(dog.color);
    }

    public String toString() {
        return "【宠物狗】名称:" + this.name + ";颜色:" + this.color;
    }
}
public class JavaDemo {
    public static void main(String [] args) {
        PetShop shop = new PetShop() ;
        shop.add(new Cat("小花猫", "彩色")) ;
        shop.add(new Cat("小白猫", "白色")) ;
        shop.add(new Dog("白狗", "浅色")) ;
        shop.add(new Dog("花狗", "深色")) ;
        Object result [] = shop.search("白").toArray() ;
        for (Object obj : result) {
            System.out.println(obj) ;
        }
    }
}

结果:

遇到的问题一:

原因:JDK是国际版的,在使用javac编译时,编译程序首先会获得操作系统的默认编码格式GBK,然后,JDK把Java源文件从GBK编码格式转换为Java内部默认的Unicode格式存入内存,再通过Javac把转换后的Unicode格式文件编译成class类(还是Unicode编码)。需要重新设置编译参数:

问题二:

首行包路径错误(可直接删除)

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值