java动物园管理代码 java动物园管理代码大全
编写Java程序,用来描述动物园里的猴子。要求如下 (1) 创建一个Monkey(猴子)类,其属性包括颜色和性别
public class Monkey {
创新互联建站专注于榆次企业网站建设,响应式网站建设,商城建设。榆次网站建设公司,为榆次等地区提供建站服务。全流程专业公司,专业设计,全程项目跟踪,创新互联建站专业和态度为您提供的服务
private String color;
private String sex;
public Monkey(String color,String sex) {
this.color = color;
this.sex = sex;
}
public Monkey(String color) {
this.color = color;
this.sex = "公";
}
public Monkey(){}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int add(int p1,int p2,int p3) {
return p1+p2+p3;
}
public int add(int p1,int p2) {
return p1+p2;
}
}
public class Zoo {
public static void main(String[] args) {
Monkey g = new Monkey("黑");
Monkey l = new Monkey("红","母");
int p1 = g.add(1, 2);
int p2 = l.add(1, 2, 3);
System.out.println(g.getSex()+":"+p1);
System.out.println(l.getSex()+":"+p2);
}
}
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 != 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(ListAnimal 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");
}
}
java代码写一个zoo类,要求生成若干个Animal.fish.和bird.并调用他们的属性和方
public class zoo{
String name;
int age;
public zoo(String a,int b){
name = a;
age = b;
}
public String getName(){
return name;
}
public int getAge(){
return age;
}
public static void main(String args[]) {
zoo animal1 = new zoo(fish,1);
zoo animal2 = new zoo(bird,2);
String m = animial1.getName();
String n = animial2.getName();
String p = animal1.age;
String q = animal2.age;
System.out.println("动物为 +m +年龄为+p+");
System.out.println("动物为 + n+年龄为+q+");
}
}
说明,本人只是自己输入的代码,并没有调试,只是告诉你一个框架或是模板,如果需要别的功能,自己添加即可。调试不过,可以在问我,我会告诉你的。
当前标题:java动物园管理代码 java动物园管理代码大全
本文路径:http://myzitong.com/article/dogedid.html