【问题标题】:Plants are JAVA objects in a 100 by 100 garden, How to check each plant?植物是 100 x 100 花园中的 JAVA 对象,如何检查每株植物?
【发布时间】:2021-05-19 16:26:45
【问题描述】:

我有一个名为 Plant 的课程。它有一个这样的构造函数。

int xPosition, yPosition;
String plantType;
int Size;


public Plant(String ptype, int s) {

    xPosition = xPos;
    yPosition = yPos;
    plantType = ptype;
    Size = s;

}

我想跟踪这些虚拟植物。一次只能在一个位置种植一株植物。我决定也许我可以使用一个数组。

Plant[][] fieldOfPlants;
    fieldOfPlants = new Plant[100][100];

    for (int i = 0; i < 100; i++) {
        for (int j = 0; j < 100; j++) {
            fieldOfPlants[i][j] = new Plant(i, j, "none", false);
        }
    }


    // Plant the seeds at these locations.
    fieldOfPlants[12][14].plantType = "noodle plant";
    fieldOfPlants[12][14].seedPlant();

这是有道理的,但是 i,j 坐标和 xPos, yPos 坐标可能不同步不是很糟糕吗?将位置设置在两个地方似乎是糟糕的设计。

如何从构造函数中消除xPos和yPos?

【问题讨论】:

  • 你是说 into 在两个地方,因为它在 Java 对象和数据库中?
  • 你的植物真的需要知道它的种植地点吗?
  • 对于“如何从构造函数中去掉xPos和yPos”这个问题的答案就是把它们从构造函数中去掉。
  • 或者如果你的植物确实需要知道它是在哪里种植的,你可以把植物数组放在某个静态类中,并添加一些实用方法以便任何植物对象都可以查找它的位置吗?
  • 查看相关example

标签: java class database-design constructor coordinates


【解决方案1】:

执行此操作的常用方法是使用static class。这本质上是一个所有内容都是静态的类,因此您无需创建此类的任何实例。在这种情况下,您可能需要一个“花园”类来跟踪您的所有植物。假设您不打算拥有多个花园,这将是您的应用程序的 花园类,这就是为什么它是一个静态类是有意义的。为了跟踪所有植物,花园类需要包含您的“植物领域”数组,并且为了有用,它需要实现一些实用方法来访问该数组。以下是我将如何实现花园类:

// Notice the final modifier.  This is standard for static classes because there is
// no point in extending them.
public final class Garden {
    // Here's your "field of plants" array.  Notice it's static, so its not bound to
    // any instance.
    private static final Plant[][] FIELD_OF_PLANTS = new Plant[100][100];

    // Notice the private constructor.  This is also standard for static classes
    // because there is no point in initializing them, since everything is static.
    private Garden() { }

    // This is a utility method to get the location of a given plant.  It just does a
    // linear search through the array and returns the location when it finds a match.
    // You can find much faster algorithms, but for demonstration purposes, linear
    // search does the job.
    public static Point getPlantLocation(Plant plant) {
        for (int i = 0; i < 100; i++) {
            for (int j = 0; j < 100; j++) {
                if (FIELD_OF_PLANTS[i][j] == plant) {
                    return new Point(i, j);
                }
            }
        }

        // If we got this far, that means plant isn't anywhere in FIELD_OF_PLANTS
        throw new NoSuchElementException();
    }

    // This is a utility method going the other way, finding which plant is at a given
    // location.  It just returns the proper index in the array.
    public static Plant getPlantAt(int x, int y) {
        return FIELD_OF_PLANTS[x][y];
    }

    // This is a mutator utility method for planting a plant in the garden.
    // It just sets the appropriate index of the array to the passed in plant object.
    public static void setPlantAt(int x, int y, Plant plant) {
        FIELD_OF_PLANTS[x][y] = plant;
    }
}

然后在你的植物类中,你可以把位置信息去掉,你仍然可以添加使用植物位置的方法:

public class Plant {
    // Notice the only thing the plant knows is its type and size.
    private int size;
    private final String plantType;

    public Plant(String ptype, int s) {
        plantType = ptype;
        size = s;
    }

    // This method uses the plant's location by calling the static class' utility
    // method.
    public void printAllInformation() {
        Point location = Garden.getPlantLocation(this);
        System.out.format("I am a %s and my size is %d.  I am at row %d, column %d.%n",
                plantType, size, location.y, location.x);
    }
}

您可以像这样使用一个简单的驱动程序类来测试所有这些:

public class Driver {
    public static void main(String args[]) {
        FIELD_OF_PLANTS[12][14] = new Plant("noodle plant", 0);
        FIELD_OF_PLANTS[12][14].printAllInformation();
    }
}

结果应该是:

I am a noodle plant and my size is 0.  I am at row 14, column 12.

这样,您在花园课程中只存储了每株植物的位置一次,但每株植物仍然可以确定它在花园中的位置。

【讨论】:

    猜你喜欢
    • 2018-11-27
    • 2016-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-22
    • 2015-11-14
    • 1970-01-01
    相关资源
    最近更新 更多