Java-在构造函数

时间:2017-01-22 20:02:29

标签: java arrays constructor

我必须编写这个具有Ant类的程序:

  • 默认构造函数将实例变量queens初始化为 只有一位女王命名为“Beth”,colonySize为100,000。

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

  • 方法显示,以格式显示有关蚂蚁的信息 下面:

    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类

  • 定义的构造函数接受两个参数并初始化相关参数 实例变量。然后它将蚂蚁的颜色设置为“红色”和 有毒的实例变量为真。

  • 方法显示,以格式显示有关火蚁的信息 下面:

    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设置为数组中的第一个元素。 我还想知道当布尔变量venomous设置为true而不是硬编码时,它是如何打印出来的。无法弄清楚如何解决这两件事。

非常感谢任何帮助。此外,我想在该计划中提出建议和改进。

感谢。

1 个答案:

答案 0 :(得分:3)

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){
相关问题