[英]Java- Looping through through an array with instance variables in constructors
我必须编写这个有Ant类的程序:
默认构造函数将实例变量queens初始化为仅1个名为“Beth”的queen和colonySize为100,000。
定义的构造函数接受两个参数并初始化相应的实例变量。
方法显示,以下面的格式显示有关蚂蚁的信息:
This ant colony has the following queens: Queen 1: Beth The colour of the ants: black The approximate colony size: 100000
这是我为此写的类蚂蚁 :
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);
}
}
然后Firetents类扩展了Ants类
定义的构造函数接受两个参数并初始化相关的实例变量。 然后它将蚂蚁的颜色设置为“红色”,将毒性实例变量设置为true。
方法显示,以下面的格式显示有关火蚁的信息:
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
类FireAnts:
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");}
}
}
使用以下内容初始化数组ants的MainAnts类:
i)一个FireAnt殖民地,两个皇后分别命名为Lilith和Maya,殖民地大小为250,000只蚂蚁。
ii)蚂蚁
并在运行时显示以下内容:
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
课程主要人员:
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设置为数组中的第一个元素。 我还想知道当设置为true
而不是硬编码时,如何将布尔变量venomous
打印出来。 无法弄清楚如何解决这两件事。
任何帮助将不胜感激。 此外,我想在该计划中提出建议和改进。
谢谢。
在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);
最后,关于你的venomous
问题,一个=
是任务; 你需要两个平等。
if(this.venomous=true){
应该
if(this.venomous==true){
或者只是
if(this.venomous){
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.