我必须编写这个具有 Ant 类的程序:
默认构造函数将实例变量 queens 初始化为 只有 1 个名为“Beth”的蜂后,colonySize 为 100,000。
定义的构造函数接受两个参数并初始化 相应的实例变量。
方法显示,以格式显示有关 Ant 的信息 下面:
This ant colony has the following queens: Queen 1: Beth The colour of the ants: black The approximate colony size: 100000
这是我为此编写的 Class Ants:
public class Ant {
private String[] queens= new String [1];
public String colour= "black";
private int colonySize;
public Ant(){
queens[0]= "Beth";
colonySize= 100000;
}
public Ant(String[] queens, int colonySize){
this.queens[0]= queens[0];
this.colonySize= colonySize;
}
public void display(){
for(int i=0; i<queens.length;i++){
System.out.println("Queen "+ (i+1) +":" + queens[i]);
}
System.out.println("The colour of the ants: "+ this.colour);
System.out.println("The approximate size of the colony: "+ this.colonySize);
}
}
然后 FireAnts 类扩展了 Ants 类
定义的构造函数接受两个参数并初始化相关的 实例变量。然后它将 Ant 的颜色设置为“红色”并且 有毒的实例变量为真。
方法显示,以格式显示有关火蚁的信息 下面:
This ant colony has the following queens: Queen 1: Lilith Queen 2: Maya The colour of the ants: red The approximate colony size: 250000 The Fire ants are: venomous
火蚁类:
public class FireAnt extends Ant {
private boolean venomous;
public FireAnt(String[] queens, int colonySize){
super(new String[]{"Lilith", "Maya"}, 250000);
super.colour= "red";
this.venomous= true;
}
public void display(){
super.display();
if(this.venomous=true){
System.out.println("The ants are: venomous");}
}
}
MainAnts 类使用以下内容初始化数组 ants:
i) 一个 FireAnt 群体,有两个分别名为 Lilith 和 Maya 的蚁后,以及 拥有 250,000 只 Ant 的蚁群。
ii) 一只 Ant
运行时显示如下:
This ant colony has the following queens:
Queen 1: Lilith
Queen 2: Maya
The colour of the ants: red
The approximate colony size: 250000
The Fire ants are: venomous
This ant colony has the following queens:
Queen 1: Beth
The colour of the ants: black
The approximate colony size: 100000
MainAnts 类:
public class MainAnts {
public static void main(String[] args) {
// TODO Auto-generated method stub
Ant obj[]= new Ant[2];
obj[0]= new Ant();
obj[1]= new FireAnt(new String[]{"Lilith", "Maya"}, 250000);
for(int i=0; i<2;i++){
obj[i].display();
System.out.println();
}
}
}
问题是,当我运行程序时,只打印了来自 FireAnts 的第一个 Queen。下面是主类运行时的输出。
This ant colony has the following queens:
Queen 1: Lilith
The colour of the ants: red
The approximate colony size: 250000
The Fire ants are: venomous
This ant colony has the following queens:
Queen 1: Beth
The colour of the ants: black
The approximate colony size: 100000
我想这是因为在 Ants 类中,我将 queen 设置为数组中的第一个元素。
我还想知道当 boolean 变量 venomous
设置为 true
而不是硬编码时如何打印出来。无法弄清楚如何解决这两件事。
如有任何帮助,我们将不胜感激。另外,我想对程序提出建议和改进。
谢谢。
最佳答案
在 Ant
构造函数中,您只需复制第一个 queen
。改变
public Ant(String[] queens, int colonySize){
this.queens[0]= queens[0];
this.colonySize= colonySize;
}
到
public Ant(String[] queens, int colonySize){
this.queens = queens;
this.colonySize= colonySize;
}
然后,改变
super(new String[]{"Lilith", "Maya"}, 250000);
到
super(queens, colonySize);
最后,关于你的毒辣
问题,一个=
是赋值;您需要两个才能实现平等。
if(this.venomous=true){
应该是
if(this.venomous==true){
或者只是
if(this.venomous){
关于Java-在构造函数中使用实例变量循环遍历数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41795550/