本文介绍了创建数组的对象的静态数组。这是好事还是坏事?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如您所见,我已经在对象Corner中创建(实例化了)Corner对象的静态数组。这是好表格吗?我希望所有的Corner对象都可以访问所有其他的Corner对象。

As you can see, I've created (instantiated?) a static array of Corner objects in the object Corner. Is this good form? I want all the Corner object to have access to all the other Corner objects.

package Main;

public class Corner {

private String biome;

private static Corner[][] corners;

private float elevation, moisture, heat;

private boolean isRiver, isLake;

private int x, y;

public void createArray(int width, int height) {

    corners = new Corner[width][height];

}

public String getBiome() { return biome; }

public void setBiome(String biome) {
    this.biome = biome;
}

public float getElevation() { return elevation; }

public void setElevation(float elevation) {
    this.elevation = elevation;
}

public float getMoisture() {
    return moisture;
}

public void setMoisture(float moisture) {
    this.moisture = moisture;
}

public float getHeat() { return heat; }

public void setHeat(float heat) { this.heat = heat; }

public boolean isRiver() {

    return false;
}

public boolean isLake() {

    return false;
}

public static Corner[][] getCorners() {
    return corners;
}
}

没有更多要添加的细节。

There are no more details to add.

推荐答案

如果角落的数量发生变化,则需要创建一个更大的新数组并将所有 Corner 从旧数组复制到新的更大数组中。这应该向您表明您可能希望拥有与数组不同的数据结构。可以像 List Set 一样增长。

If the amount of Corners changes you need to create a new bigger array and copy all the Corners from the old array to the new bigger one. This should indicate to you that you might want to ave a different data structure than an array. One that can grow like a List or a Set.

通常,角落不需要知道其他角落。不同的类型应该管理所有 Corner 并处理它们之间的依赖关系。

In general, a Corner should not need to know of other Corners. A different type should manage all the Corners and handle dependencies between them.

您没有写为什么'我希望所有 Corner 对象都具有访问权限到所有其他角落对象,因此我不建议使用这种管理类型。

You did not wrote why 'I want all the Corner object to have access to all the other Corner objects' so I cannot recommend how this managing type could look like.

这篇关于创建数组的对象的静态数组。这是好事还是坏事?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 22:05