植物是 100 x 100 花园中的 JAVA 个物体,如何检查每株植物?
Plants are JAVA objects in a 100 by 100 garden, How to check each plant?
我有一个 class 叫做 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?
一种常见的方法是使用 static class。这本质上是一个 class,其中所有内容都是静态的,因此您不会创建此 class 的任何实例。在这种情况下,您可能需要一个“花园”class 来跟踪您所有的植物。假设您不打算拥有多个花园,这将是您的应用程序的 the 花园 class,这就是为什么它是静态的 [=27] 是有意义的=].为了跟踪所有植物,花园 class 需要包含您的“植物田”数组,并且为了有用,它需要实现一些实用方法来访问该数组。这是我将如何实现花园 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;
}
}
然后在你的工厂class中,你可以去掉位置信息,你仍然可以添加使用工厂位置的方法:
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);
}
}
您可以使用简单的驱动程序 class 测试所有这些,如下所示:
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.
这样您只存储了每株植物在您花园中的位置一次class,但每株植物仍然可以确定它在花园中的位置。
我有一个 class 叫做 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?
一种常见的方法是使用 static class。这本质上是一个 class,其中所有内容都是静态的,因此您不会创建此 class 的任何实例。在这种情况下,您可能需要一个“花园”class 来跟踪您所有的植物。假设您不打算拥有多个花园,这将是您的应用程序的 the 花园 class,这就是为什么它是静态的 [=27] 是有意义的=].为了跟踪所有植物,花园 class 需要包含您的“植物田”数组,并且为了有用,它需要实现一些实用方法来访问该数组。这是我将如何实现花园 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;
}
}
然后在你的工厂class中,你可以去掉位置信息,你仍然可以添加使用工厂位置的方法:
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);
}
}
您可以使用简单的驱动程序 class 测试所有这些,如下所示:
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.
这样您只存储了每株植物在您花园中的位置一次class,但每株植物仍然可以确定它在花园中的位置。