2017-09-29 29 views
3

我目前使用Java和Codename One構建了一個2D小行星遊戲,主要關注OOP模式。然而,我遇到了麻煩,試圖動態地創建新對象(在我的例子中,我想添加的對象是一個超類GameObject)以添加到ArrayList。正如你在GameWorld.java的void init()方法中看到的那樣,我創建了一個列表,其中包含包含Asteroids,Ships,Spacestations的子類的GameObject類型。如何在Java中動態創建要添加到ArrayList中的對象

該程序要求在類的命令鍵盤輸入,如'a',然後應該添加一個新的Asteroid(GameObject的子類)對象到ArrayList中。用戶應該能夠添加儘可能多的Asteroids,因爲他們想分配給ArrayList。

我的問題是:我如何做到這一點沒有已經宣佈了一個名爲AsteroidTest型小行星的變量像我當前已經在我的addAsteroid做類遊戲世界的()函數?謝謝!

GameWorld.java

package com.mycompany.a1; 

import java.util.ArrayList; //For ArrayList Usage 

public class GameWorld { 

public void init() { 
    ArrayList<GameObject> list = new ArrayList<GameObject>(); 
} 
//other methods here to manipulate Game objects and data 
public void addShip() { 
    Ship ShipTest = new Ship(); 

    list.add(ShipTest); 

    System.out.println(ShipTest.getLocation()); 
} 

public void addAsteroid(){ 
    Asteroid AsteroidTest = new Asteroid(); 

    list.add(AsteroidTest); 


    System.out.println(AsteroidTest.getLocation()); 
} 


public void addSpaceStation(){ 

} 
} 

Game.java

package com.mycompany.a1; 

import com.codename1.ui.events.ActionListener; 
import com.codename1.ui.Label; 
import com.codename1.ui.TextField; 
import com.codename1.ui.events.ActionEvent; 
import java.lang.String; 
import com.codename1.ui.Form; 

public class Game extends Form{ 
private GameWorld gw; 

public Game(){ 
    gw = new GameWorld(); 
    gw.init(); 
    play(); 

} 

private void play(){ 

    Label myLabel=new Label("Enter a Command:"); this.addComponent(myLabel); 
    final TextField myTextField=new TextField(); 
    this.addComponent(myTextField); 
    this.show(); 
    myTextField.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent evt) { 
      String sCommand=myTextField.getText().toString(); 
      myTextField.clear(); 
      switch (sCommand.charAt(0)){ 
      case 's': 
       gw.addShip(); 
       break; 
      case 'a': 
       gw.addAsteroid(); 
       break; 
      case 'b': 
       gw.addSpaceStation(); 
       break; 
      //add code to handle rest of the commands 
      } //switch 
     } //actionPerformed 
    } //new ActionListener() 
      ); //addActionListener 
    } //play 
    //code to enter text field and receive keyboard input 
} 

GameObject.java超類。子類包括小行星,船,飛彈,將SpaceStation

package com.mycompany.a1; 

public abstract class GameObject { 
private double x; 
private double y; 

public GameObject(){ 
    x = 0; 
    y = 0; 
} 

public double getX(){ 
    return x; 
} 

public void setX(double newX){ 
    x = newX; 
} 

public double getY(){ 
    return y; 
} 

public void setY(double newY){ 
    y = newY; 
} 

public String getLocation(){ 
    return "(" + x + ", " + y + ")"; 

} 
} 

Asteroid.java

package com.mycompany.a1; 

import java.util.Random; 

public class Asteroid extends MovableObject { 

private Random rand = new Random(); 


public Asteroid() { 
    setX(rand.nextInt(1023) + rand.nextDouble()); 

    setY(rand.nextInt(767) + rand.nextDouble());// TODO Auto-generated 
    constructor stub 
    } 

} 
+1

'list.add(新小行星())更清晰;' –

+0

@KevinAnderson哇真的那麼簡單嗎?我也得到ArrayList列表變量的錯誤。錯誤說「局部變量列表的值未被使用」。 –

+0

應該說'list'沒有被定義。你在'init()'方法的本地定義了'list',但是這個定義在'init()'之外是不可見的。你希望'list'被定義爲你類的成員變量。如果你不明白爲什麼,你需要研究的主題是「變量的範圍」。 –

回答

1

這是很簡單的:

public void addAsteroid(){ 
    this.list.add(new Asteroid()); 
} 

,但如果你想打印位置,必須在創建新的小行星時做,因此在構造函數中:

public GameObject(){ 
    x = 0; 
    y = 0; 
    System.out.println("(" + x + ", " + y + ")"); 
} 

此外:

  • 聲明ArrayList<GameObject> listList<GameObject>使用抽象NOT具體實施
  • 在遊戲世界一流的聲明List<GameObject> list私人領域
  • 考慮構造的工作而不是用public void init()
  • 的情況下,的問題,請使用java.util.concurrent.CopyOnWriteArrayList<E>代替傳統清單
  • 考慮到使用在第二遊戲物體構造,處理的對象的創建與initaial cordinates +除去setX的和SETY方法+介紹moveTo(double x, double y)方法以與意圖
+0

@maciejlesiak謝謝你的建議!然而,我不明白你的第一點。利用List 與使用ArrayList 列表相反的確切adantange是什麼?而且我肯定會爲X和Y座標添加第二個構造函數,這比我目前的智能要聰明得多。 –

+0

通常的做法是使用抽象(接口,抽象類等)而不是特定的實現作爲變量類型。這樣,你可以隱藏實現(因此如果你在實現中使用ArrayList或LinkedList或其他List,你的類的客戶端並不關心)。什麼是最重要的是你使用一些列表,這就是爲什麼應該有一個接口。 如果您想了解更多信息,請參閱SOLID原則,特別是DIP https://en.wikipedia.org/wiki/Dependency_inversion_principle – maciejlesniak

相關問題