Possible Duplicate:
Decoupling Class from Wolf and SheepJava解耦类和移动方法
嘿那里。我对Java很新,所以对于不懂的理解我很抱歉:)我会尽我所能解释这个问题。我有一个我一直在尝试编辑的项目。 java项目涉及模拟狼和绵羊,因为它们共存并且吃掉羊。我有各种各样的课程,但我关注的主要课程是Simulator和AnimalFactory。有一个狼和羊班,但他们并不特别重要的问题。模拟器类在其中一种方法中使用绵羊和狼数据。我试图将该方法移动到另一个名为AnimalFactory的类,以便模拟器只依赖动物列表,而不依赖于单个的狼和绵羊。我也一直试图修改Simulation类中的构造函数,并让它们将AnimalFacotry对象作为参数,但遇到问题。当我将填充方法移入AnimalFactory并尝试编译Simulator类时,出现以下错误:无法找到符号 - 方法填充。我认为这是因为该方法是在不同的类中,但是当我引用Simulator.populate方法时,它说它不能从静态对象引用。任何帮助获得在AnimalFactory类中工作的方法和更改模拟器构造函数,以便将填充方法考虑在内的任何帮助都将不胜感激。这是目前为止AnimalFactory类的代码。下面将模拟器代码
import java.awt.Color;
public class AnimalFactory
{
/**
* Constructor for objects of class AnimalFactory
*/
public AnimalFactory()
{
}
/**
* Randomly populate the field with woolfs and a sheep.
*/
public void populate()
{
field.clear();
Sheep sheep = new Sheep(field,field.freeRandomLocation());
animals.add(sheep);
for(int no = 1; no <= 3; no++) {
Wolf wolf = new Wolf(field,field.freeRandomLocation(),sheep);
animals.add(wolf);
}
}
/**
* Populate the simulation with animals and connect to view.
* @param view The visualisation of the simulation.
*/
public void produce(SimulatorView view)
{
// Associate colors with the animal classes.
view.setColor(Sheep.class, Color.green);
view.setColor(Wolf.class, Color.red);
}
}
import java.util.Random;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.awt.Color;
*/
public class Simulator
{
// Constants representing configuration information for the simulation.
// The default width for the grid.
private static final int DEFAULT_WIDTH = 70;
// The default depth of the grid.
private static final int DEFAULT_DEPTH = 40;
// List of animals in the field.
private List<Animal> animals;
// The current state of the field.
private Field field;
// The current step of the simulation.
private int step;
// A graphical view of the simulation.
private SimulatorView view;
// A factory for creating animals - unused as yet.
private AnimalFactory factory;
/**
* Construct a simulation field with default size.
*/
public Simulator()
{
this(DEFAULT_DEPTH, DEFAULT_WIDTH);
}
/**
* Create a simulation field with the given size.
* @param depth Depth of the field. Must be greater than zero.
* @param width Width of the field. Must be greater than zero.
*/
public Simulator(int depth, int width)
{
if(width <= 0 || depth <= 0) {
System.out.println("The dimensions must be greater than zero.");
System.out.println("Using default values.");
depth = DEFAULT_DEPTH;
width = DEFAULT_WIDTH;
}
animals = new ArrayList<Animal>();
field = new Field(depth, width);
// Create a view of the state of each location in the field.
view = new SimulatorView(depth, width);
factory = new AnimalFactory();
// Setup a valid starting point.
reset();
}
/**
* Run the simulation from its current state for a reasonably long period,
* e.g. 500 steps.
*/
public void runLongSimulation()
{
simulate(500);
}
/**
* Run the simulation from its current state for the given number of steps.
* Stop before the given number of steps if it ceases to be viable.
* @param numSteps The number of steps to run for.
*/
public void simulate(int numSteps)
{
for(int step = 1; step <= numSteps && view.isViable(field); step++) {
simulateOneStep();
}
}
/**
* Run the simulation from its current state for a single step.
* Iterate over the whole field updating the state of each
* fox and rabbit.
*/
public void simulateOneStep()
{
step++;
// Provide space for newborn animals.
List<Animal> newAnimals = new ArrayList<Animal>();
// Let all rabbits act.
for(Iterator<Animal> it = animals.iterator(); it.hasNext();) {
Animal animal = it.next();
animal.act(newAnimals);
if(! animal.isAlive()) {
it.remove();
}
}
// Add the newly born foxes and rabbits to the main lists.
animals.addAll(newAnimals);
view.showStatus(step, field);
// Pause for 200 milliseconds
try {
Thread.sleep(200);
} catch(InterruptedException e) {
}
}
/**
* Reset the simulation to a starting position.
*/
public void reset()
{
step = 0;
animals.clear();
AnimalFactory.populate();
factory.produce(view);
// Show the starting state in the view.
view.showStatus(step, field);
}
}
完全相同的人提出了完全相同的问题。 – DJClayworth 2011-03-02 20:33:25