2011-03-02 2 views
0

Possible Duplicate:
Decoupling Class from Wolf and SheepJava 디커플링 클래스 및 이동 방법

안녕하세요. 나는 자바에 대해 상당히 익숙해 져서 noobish에 대한 사과를하고 있습니다 :) 나는 가능한 한 할 수있을뿐만 아니라 문제를 설명하려고 노력할 것입니다. 나는 내가 편집하려고 노력하고있는 프로젝트를 가지고있다. 자바 프로젝트는 늑대와 양의 공존과 양의 시뮬레이션을 포함합니다. 다양한 클래스가 있지만 주요한 것들은 Simulator와 AnimalFactory입니다. 늑대와 양 클래스가 있지만 그들은 그 문제에 특히 중요하지 않습니다. 시뮬레이터 클래스는 방법 중 하나에서 양과 늑대 데이터를 사용합니다. 그 방법을 AnimalFactory라는 다른 클래스로 옮기려고합니다. 그래서 Simulator는 동물리스트에만 의존하고 개별 늑대와 양에는 의존하지 않습니다. 또한 Simulation 클래스의 생성자를 수정하고 AnimalFacotry 객체를 매개 변수로 사용하도록 설정했지만 문제가 발생했습니다. Populate 메서드를 AnimalFactory로 이동하고 Simulator 클래스를 컴파일하려고하면 다음 오류가 발생합니다. 심볼을 찾을 수 없습니다 - 메서드가 채워집니다. 이 방법은 다른 클래스에 있지만 Simulator.populate 메서드를 참조 할 때 정적 개체에서 참조 할 수 없다고 가정합니다. AnimalFactory 클래스에서 작동하는 메소드를 얻고 시뮬레이터 생성자를 변경하여 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

답변

관련 문제