Java-在构造函数中通过带有实例变量的数组循环

Java-在构造函数中通过带有实例变量的数组循环,java,arrays,constructor,Java,Arrays,Constructor,我必须编写这个程序,它的类为Ant: 默认构造函数将实例变量初始化为queen 只有1名女王被命名为“贝丝”,并被克隆到100000名 定义的构造函数接受两个参数并初始化 对应的实例变量 方法显示以以下格式显示有关ant的信息: 下: 这是我为之写的类蚂蚁: public class Ant { private String[] queens= new String [1]; public String colour= "black"; private int colonySize;

我必须编写这个程序,它的类为Ant:

  • 默认构造函数将实例变量初始化为queen 只有1名女王被命名为“贝丝”,并被克隆到100000名

  • 定义的构造函数接受两个参数并初始化 对应的实例变量

  • 方法显示以以下格式显示有关ant的信息: 下:

这是我为之写的类蚂蚁

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);
    }
}
类灭火剂:

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");}
    }

}
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();
}


 }
}
类MainAnts使用以下命令初始化数组ants:

i) 一个火蚁群落,有两个王后,分别叫莉莉丝和玛雅,还有 有250000只蚂蚁的群体

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 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");}
    }

}
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();
}


 }
}
我想这是因为在Ants类中,我将queen设置为数组中的第一个元素。 我还想知道当布尔变量
venous
设置为
true
而不是硬编码时,它是如何打印出来的。不知道如何解决这两件事

任何帮助都将不胜感激。此外,我想在该计划的建议和改进


谢谢。

Ant
构造函数中,您只需复制第一个
皇后
。改变

public Ant(String[] queens, int colonySize){
    this.queens[0]= queens[0];
    this.colonySize= colonySize;
}

还有,改变

super(new String[]{"Lilith", "Maya"}, 250000);

最后,关于你的
毒辣的
问题,一个
=
是作业;你需要两个人才能平等

if(this.venomous=true){
应该是

if(this.venomous==true){
只是

if(this.venomous){

艾略特的回答应该能解决你的问题。考虑将你的类重命名为<代码> ANTROCKONE/COD>和<代码> FieltCabor < /Cord>。因为这些对象实际上并不代表一个蚂蚁。“米迦勒谢谢你的建议:”谢谢你的上述解决方案。实际上,对于有毒的部分,我的意思是如何像这样打印它
System.out.println(“蚂蚁是:+this.venous的)
。好吧,如果可以这样访问布尔值。@Diksha您可以使用
System.out.println(“蚂蚁是:+(this.venous?):“not venemous”)
if(this.venomous=true){
if(this.venomous==true){
if(this.venomous){