-5

类块

public class block {    
public enum size {SMALL,MEDIUM,LARGE,XLARGE}
    public String color;public size sz;

public block(String color, size sz) {
    super();
    this.color = color;
    this.sz = sz;
}
     public block() {
    this.color="White";
    this.sz=sz.SMALL;

}

public String toString() {
    String result="My color is " + color + " and size=" + sz;
    return result;}

类 rowOfBlock

public class rowofBlocks extends block{

        private block[] blocks;
private int numofBlocks;

public rowofBlocks(int numofBlocks){
    //super();
    this.numofBlocks=numofBlocks;
    generateBlocks() ;
}

private void generateBlocks() {

    this.blocks=new block[numofBlocks];

}

public String toString() {
    for(int a=0;a<blocks.length;a++)
    {

    System.out.print( blocks[a]);
    }
    String a="d";
    return a;
}
}

类游戏主

public class gameMain {

    public static void main(String[] args) {



block b=new block();
b.setSz(block.size.LARGE);
b.setColor("WHITE");

    rowofBlocks myrows=new rowofBlocks(3);
    System.out.println(b);
    System.out.println(myrows);
    }}

我的意思是当我尝试创建块数组时,我只看到 nullnullnulld 而不是 My color is White。

4

1 回答 1

0

你得到null值是因为你没有Block在你的数组中创建任何类对象RowOfBlocks

使用以下命令创建数组后:

this.blocks = new block[numofBlocks];

在方法体中generateBlocks()

您必须对其进行迭代并插入实际块:

for(int i=0 ; i < this.blocks.length ; i++) {
     this.blocks[i] = new Block();
}
于 2012-05-26T12:01:25.717 回答