一道JAVA题,在线等答案,求高手帮忙~

2025-01-05 00:14:21
推荐回答(2个)
回答1:

public class Test {

public static void main(String[] args) {

IBird chicken1 = new Chicken("Chicken 1", "Red", 3.5F);
IBird chicken2 = new Chicken("Chicken 1", "Green", 11.5F);
IBird chicken3 = new Chicken("Chicken 1", "Blue", 7.5F);
IBird eagle1 = new Eagle("Eagle 1", "Black", 100F);
IBird eagle2 = new Eagle("Eagle 2", "White", 35F);

System.out.println("Chicken 1 can reach 10 height? " + chicken1.isReachable(10));
System.out.println("Chicken 2 can reach 10 height? " + chicken2.isReachable(10));
System.out.println("Chicken 3 can reach 10 height? " + chicken3.isReachable(10));
System.out.println("Eagle 1 can reach 10 height? " + eagle1.isReachable(10));
System.out.println("Eagle 2 can reach 10 height? " + eagle2.isReachable(10));

if(chicken1.isReachable(10F)){
chicken1.printBirdInformation();
}

if(chicken2.isReachable(10F)){
chicken2.printBirdInformation();
}

if(chicken3.isReachable(10F)){
chicken3.printBirdInformation();
}

if(eagle1.isReachable(10F)){
eagle1.printBirdInformation();
}

if(eagle2.isReachable(10F)){
eagle2.printBirdInformation();
}
}
}

interface IBird{
public void setName(String name);
public void setColor(String color);
public void setMaxFlyingHeight(float height);
public boolean isReachable(float height);
public void printBirdInformation();
}

class Chicken implements IBird{
private String name;
private String color;
private float maxFlyingHeight;

private int eggs;
public Chicken(String name, String color, float maxFlyingHeight){
this.name = name;
this.color = color;
this.maxFlyingHeight = maxFlyingHeight;
}

public int getEggs() {
return eggs;
}
public void setEggs(int eggs) {
this.eggs = eggs;
}

public boolean isReachable(float height) {
return height >= maxFlyingHeight;
}

public void printBirdInformation() {
System.out.println("Chicken's information: name: " + this.name + ", color: " + this.color);
}

public void setColor(String color) {
this.color = color;
}

public void setMaxFlyingHeight(float height) {
this.maxFlyingHeight = height;
}

public void setName(String name) {
this.name = name;
}
}

class Eagle implements IBird{
private String name;
private String color;
private float maxFlyingHeight;

private double flyingDistance;

public Eagle(String name, String color, float maxFlyingHeight){
this.name = name;
this.color = color;
this.maxFlyingHeight = maxFlyingHeight;
}

public double getFlyingDistance() {
return flyingDistance;
}

public void setFlyingDistance(double flyingDistance) {
this.flyingDistance = flyingDistance;
}

public boolean isReachable(float height) {
return height >= maxFlyingHeight;
}

public void printBirdInformation() {
System.out.println("Eagle's information: name: " + this.name + ", color: " + this.color);
}

public void setColor(String color) {
this.color = color;
}

public void setMaxFlyingHeight(float height) {
this.maxFlyingHeight = height;
}

public void setName(String name) {
this.name = name;
}
}

回答2:

我晚来一步,楼上又神速了。