
import java.util.ArrayList
import java.util.List
class Animal {
public 镇如String name
public String color
public String toString() {
return " Name: " + this.name + " Color: " + this.color
}
/**
* 动物吃东西
*
* @param foodName
* 食物名称
*/
public void eat(String foodName) {
}
}
class Dog extends Animal {
public Dog() {
}
public Dog(String name, String color) {
this.name = name
this.color = color
}
public void eat(String foodName) {
System.out.println("Dog: " + this.name + " like to eat: " + foodName)
}
}
class Cat extends Animal {
public Cat() {
}
public Cat(String name, String color) {
this.name = name
this.color = color
}
public void eat(String foodName) {
System.out.println("Cat: " + this.name + " like to eat: " + foodName)
}
}
class Tiger extends Animal {
public Tiger() {
}
public Tiger(String name, String color) {
this.name = name
this.color = color
}
public void eat(String foodName) {
System.out.println("Tiger: " + this.name + " like to eat: " + foodName)
}
}
class Lion extends Animal {
public Lion() {
}
public Lion(String name, String color) {
this.name = name
this.color = color
}
public void eat(String foodName) {
System.out.println("Tiger: " + this.name + " like to eat: " + foodName)
}
}
public class Zoo {
public static List<Animal> zooList = new ArrayList<Animal>()
/**
* 添加Animal对象
*
* @param obj
*/
public void addAnimal(Animal obj) {
zooList.add(obj)
}
/**
* 根据animal的名字删除对象
*
* @param obj
* Animal对象
*/
public void deleteAnimal(Animal obj) {
boolean deleteFlag = false
for (int index = 0 null != zooList && index < zooList.size() index++) {
if (zooList.get(index).name.equals(obj.name)) {
zooList.remove(index)
deleteFlag = true
System.out.println("删除: " + obj + " 成功")
}
}
if (!deleteFlag) {
System.out.println("找不樱升到该动物: " + obj)
}
}
/**
* 更新Animal对象信息
*
* @param obj
* 御颂启 Animal对象
*/
public void updateAnimal(Animal obj) {
boolean flag = false
for (int index = 0 null != zooList && index < zooList.size() index++) {
if (zooList.get(index).name.equals(obj.name)) {
zooList.get(index).name = obj.name
zooList.get(index).color = obj.color
flag = true
System.out.println("\n修改成功")
}
}
if (!flag) {
System.out.println("找不到该动物: " + obj)
}
}
/**
* 根据动物名字查询
*
* @param animalName
* 动物名字
*/
public void checkAniaml(String animalName) {
boolean flag = false
for (int index = 0 null != zooList && index < zooList.size() index++) {
if (zooList.get(index).name.equals(animalName)) {
System.out.println(zooList.get(index))
flag =true
}
}
if (!flag) {
System.out.println("找不到该动物: " + animalName)
}
}
public void display(List<Animal> list) {
for (int index = 0 null != list && index < list.size() index++) {
System.out.println("\n" + list.get(index))
}
}
public static void main(String[] args) {
Zoo zoo = new Zoo()
zoo.addAnimal(new Dog("dog1", "black"))
zoo.addAnimal(new Dog("dog2", "black"))
zoo.addAnimal(new Lion("Lion1", "black"))
zoo.addAnimal(new Lion("Lion2", "black"))
zoo.addAnimal(new Cat("cat1", "yellow"))
zoo.addAnimal(new Cat("cat2", "yellow"))
zoo.addAnimal(new Tiger("Tiger1", "yellow"))
zoo.addAnimal(new Tiger("Tiger2", "yellow"))
zoo.updateAnimal(new Dog("dog1","somethingselse"))
zoo.display(zooList)
zoo.deleteAnimal(new Dog("dog2",""))
zoo.display(zooList)
zoo.checkAniaml("Lion1")
}
}
自己想要的代码都可以在钉含滚钉上玩游戏银卜。1、进入聊天界面,点击输入锋老穗框上方的三个点。
2、在打开的菜单中选择发送代码片段,选择要发送代码的类型,编辑自己想
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)