2011-03-02 68 views
0

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); 
} 
} 
+1

完全相同的人提出了完全相同的問題。 – DJClayworth 2011-03-02 20:33:25

回答

1

我相信你需要使你的animalFactory類是靜態的。