使用JAVA先说明由几个类;然后确定类和类(接口)的关系,画出类图;再编程来实现模拟一下开心牧场;农场中有农户,各种动物(牛,马,羊,鸡等),农户可以喂养动物;
时间: 2023-09-26 18:13:36 浏览: 199
首先,我们需要创建以下类:
1. Farm:农场类,包含农户和动物。
2. Farmer:农户类,可以喂养动物。
3. Animal:动物类,包括牛、马、羊和鸡。
类图如下:
```
+----------------+ +----------------+ +----------------+
| Farm | | Farmer | | Animal |
+----------------+ +----------------+ +----------------+
| - farmers: List<Farmer> | | - name: String | | - name: String |
| - animals: List<Animal> | +----------------+ | - type: String |
+-----------------------+ | + feed(animal:Animal): void | +----------------+
+--------------------------+
```
接下来,我们来编写代码实现模拟开心牧场:
```
// Animal.java
public abstract class Animal {
private String name;
private String type;
public Animal(String name, String type) {
this.name = name;
this.type = type;
}
public String getName() {
return name;
}
public String getType() {
return type;
}
public abstract void makeSound();
}
// Cow.java
public class Cow extends Animal {
public Cow(String name) {
super(name, "cow");
}
@Override
public void makeSound() {
System.out.println("Moo!");
}
}
// Horse.java
public class Horse extends Animal {
public Horse(String name) {
super(name, "horse");
}
@Override
public void makeSound() {
System.out.println("Neigh!");
}
}
// Sheep.java
public class Sheep extends Animal {
public Sheep(String name) {
super(name, "sheep");
}
@Override
public void makeSound() {
System.out.println("Baa!");
}
}
// Chicken.java
public class Chicken extends Animal {
public Chicken(String name) {
super(name, "chicken");
}
@Override
public void makeSound() {
System.out.println("Cluck!");
}
}
// Farmer.java
public class Farmer {
private String name;
public Farmer(String name) {
this.name = name;
}
public void feed(Animal animal) {
System.out.println(name + " is feeding " + animal.getName() + " the " + animal.getType());
}
}
// Farm.java
import java.util.ArrayList;
import java.util.List;
public class Farm {
private List<Farmer> farmers;
private List<Animal> animals;
public Farm() {
farmers = new ArrayList<>();
animals = new ArrayList<>();
}
public void addFarmer(Farmer farmer) {
farmers.add(farmer);
}
public void addAnimal(Animal animal) {
animals.add(animal);
}
public void feedAnimals() {
for (Farmer farmer : farmers) {
for (Animal animal : animals) {
farmer.feed(animal);
}
}
}
}
// Main.java
public class Main {
public static void main(String[] args) {
Farm farm = new Farm();
Farmer farmer1 = new Farmer("John");
Farmer farmer2 = new Farmer("Jane");
farm.addFarmer(farmer1);
farm.addFarmer(farmer2);
Animal cow1 = new Cow("Bessie");
Animal cow2 = new Cow("Maggie");
Animal horse1 = new Horse("Black Beauty");
Animal sheep1 = new Sheep("Dolly");
Animal sheep2 = new Sheep("Molly");
Animal chicken1 = new Chicken("Chickie");
farm.addAnimal(cow1);
farm.addAnimal(cow2);
farm.addAnimal(horse1);
farm.addAnimal(sheep1);
farm.addAnimal(sheep2);
farm.addAnimal(chicken1);
farm.feedAnimals();
}
}
```
以上代码模拟了开心牧场,包括农户和动物。运行程序将输出以下内容:
```
John is feeding Bessie the cow
John is feeding Maggie the cow
John is feeding Black Beauty the horse
John is feeding Dolly the sheep
John is feeding Molly the sheep
John is feeding Chickie the chicken
Jane is feeding Bessie the cow
Jane is feeding Maggie the cow
Jane is feeding Black Beauty the horse
Jane is feeding Dolly the sheep
Jane is feeding Molly the sheep
Jane is feeding Chickie the chicken
```
阅读全文