Java-在构造函数中使用实例变量循环遍历数组

标签 java arrays constructor

我必须编写这个具有 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/

相关文章:

java - 如何在 Android Studio 调试中生成打印消息?

java - 如何通过 JMS API 设置 MQMD ApplicationID 字段?

arrays - 创建另一个表和在 postgres 中使用数组有什么区别

scala - 覆盖成员和惰性 val

java - 关于 Java 中的多个构造函数

.net - 单元测试构造函数时的多个断言?

java - Gerrit 删除需要验证 +1(已验证)

java - Android Studio 中的某些 Java 文件未显示在 GitHub Desktop 上

c# - 如何拆分字节数组

c++ - 我如何确定我是否可以将数组中的某些元素相加为 K?