java编程几个类 , 可以实现动物园中几个科下动物的增删改查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 ListAnimal zooList = new ArrayListAnimal();
/**
* 添加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 != zooListindexzooList.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 != zooListindexzooList.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 != zooListindexzooList.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(ListAnimal list) {
for (int index = 0; null != listindexlist.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"));
推荐阅读
- 主播直播房间对比,直播的房间应该怎么装修
- oracle函数截取字符串,oracle 截取字符串中指定位置
- 益智游戏解说搞笑视频,益智游戏解说搞笑视频大全
- 即时战略游戏荣誉值在哪看,即时战略游戏大作
- go语言调用c类型转换 go 调用c
- 秀米怎么投屏到公众号上面,秀米怎么发起投票
- 亚马逊如何推广产品,亚马逊平台如何推广产品?
- 小米视频号直播,小米视频号直播怎么投屏
- php取数据库时间函数 php取时间间隔