Java 给出类Fish、Lake的声明,具有UML图中的成员变量和成员方法。
发布网友
发布时间:2023-08-03 15:08
我来回答
共1个回答
热心网友
时间:2024-11-09 04:42
代码如下:
class Fish {
private int weight;
public Fish(int weight) {
this.weight = weight;
}
// 增加体重
public void increaseWeight(int weight) {
this.weight += weight;
}
public int getWeight() {
return this.weight;
}
}
class Lake {
private Fish fish;
public void setFish(Fish fish) {
this.fish = fish;
}
public void feedFish(int val) {
this.fish.increaseWeight(val);
}
}
public class Test {
public static void main(String[] args) {
Fish blackFish = new Fish(1);
Lake taiLake = new Lake();
taiLake.setFish(blackFish);
taiLake.feedFish(20);
System.out.println("鱼重:" + blackFish.getWeight());
taiLake.feedFish(20);
System.out.println("鱼重:" + blackFish.getWeight());
}
}
追问
这个是运行不了嘛