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
*/
【java动物园系统源代码 java 动物园】 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"));
推荐阅读
- 一师一优课怎么下载ppt,一师一优课平台上的课件怎么下载
- 怎么找不到u盘里的信息,在电脑上怎么找不到u盘
- 微擎更换服务器,微擎对服务器的要求
- 怎么把pdf弄到苹果手机,怎么把pdf弄到苹果手机上面
- php数据库存放在哪里 php的数据库在哪个目录下
- 关于css3水波扩散动画的信息
- 复古的平面动作游戏下载,复古风平面设计
- 新媒体tv如何发展,新媒体视域下电视发展的未来想象
- 胖人角色扮演游戏,可以把游戏角色变胖的游戏