簡體   English   中英

使用 java 中另一個 class 的構造函數在不同的類中創建對象

[英]using constructor from another class in java to create objects in separate classs

我如何使用 java 中另一個 class 的構造函數通過單獨的 ZA2F2ED4F8EBC2CBBDZC62A 中的方法制作 object。 例如下面是播放器 class 中的構造函數

public class Player extends Entity {



public Player(int maxEnergy, int x, int y) {
        this.maxEnergy = maxEnergy;
        this.energy = maxEnergy;
        carryingGhost = false;
        xPos = x;
        yPos = y;
    }

我想通過一個名為的方法使用和創建對象(播放器)

private Player createPlayer() { 

並且上述方法在單獨的 class 中為

public class GameEngine {


**The method must return a Player object that represents the player in the 
game.  it must set the maxEnergy for the player, and the
X and Y positions corresponding to a tile position in the current level.    

我試圖用參數和不帶參數的方法初始化播放器**

Player player = new Player(int maxEnergy, int x, int y);

    this.player.getEnergy();
    this.player.getMaxEnergy();
    this.player.setPosition(x, y);
    
       return player;       
    }

但它會給出錯誤。任何幫助將不勝感激。我非常接近假設不可能創建這樣的對象。

下面我分享了與其他類一起使用的完整游戲引擎。

import java.awt.Point;
import java.util.ArrayList;
import java.util.Random;


public enum TileType {
        WALL, FLOOR1, FLOOR2, BANK, BREACH, DOOR;
    }


public static final int LEVEL_WIDTH = 35;

public static final int LEVEL_HEIGHT = 18;

private Random rng = new Random();

private int levelNumber = 1;  //current level

   
private int turnNumber = 1;

private GameGUI gui;

private TileType[][] level;

private ArrayList<Point> spawnLocations;

   
private Player player;

   
private Ghost[] ghosts;

   
public GameEngine(GameGUI gui) {
    this.gui = gui;
}
private TileType[][] generateLevel() {
    //YOUR CODE HERE
  
    return null;        //change this to return the 2D arrayof TileType 
                        //values that you create above
}
private ArrayList<Point> getSpawns() {
    ArrayList<Point> s = new ArrayList<Point>();
    // YOUR CODE HERE
    return s;
}
   
private Ghost[] addGhosts() {
    //YOUR CODE HERE
    
    return null;       //change this to return an array of ghost objects 
}

**/**
 * Creates a Player object in the game. The method instantiates
 * the Player class and assigns values for the energy and position.
 * The first version of this method should use fixed a fixed position 
 for  the player to start, by setting fixed X and Y values when calling 
 the constructor in the Player class. The second version of this method 
 should use the spawns ArrayLis to select a suitable location to spawn 
 the player and removes the Point from the spawns ArrayList. This will 
 prevent the Player from being added to the game inside a wall, bank or 
 breach for example. 
 
  @return A Player object representing the player in the game
 */**

private Player createPlayer() {
    //YOUR CODE HERE
    
    return null;        //change this to return a Player object
}
    
    public void movePlayerLeft() {
        
    }

    public void movePlayerRight() {
        
    }

    
    public void movePlayerUp() {
        
    }

   
    public void movePlayerDown() {
        
    }

  
    private void hitGhost(Ghost g) {
        
    }
    private void moveGhosts() {
        
    }

   
    private void moveGhost(Ghost g) {
        
    }

    
    private void cleanDefeatedGhosts() {
        
    }

   
    private void nextLevel() {
        
    }

   
    private void placePlayer() {
        
    }

    public void doTurn() {
        cleanDefeatedGhosts();
        moveGhosts();
        gui.updateDisplay(level, player, ghosts);
    }

    public void startGame() {
        level = generateLevel();
        spawnLocations = getSpawns();
        ghosts = addGhosts();
        player = createPlayer();
        gui.updateDisplay(level, player, ghosts);
    }
}

這樣的事情對你的情況有用嗎?

public class GameEngine {
  private Player createPlayer() {
    return new Player(1,2,3);
  }
}

在播放器 class 中添加默認的無參數構造函數。 一旦您使用 Arg 創建了構造函數,java 將不會自動提供默認值。

你已經聲明了 Player

private Player player;

所以你不能嘗試使用相同的變量名重新初始化,而是

private Player createPlayer() {
  Player newPlayer = new Player();
  // set the different props of the Player obj
return newPlayer ;        

}

您面臨的錯誤是什么? 你能分享一下嗎?

以下應該做到這一點:

private Player createPlayer() { 
   int defaultMaxEnergy = 10;  // Whatever value it should have
   int initialX = 1;  // Whatever value it should have
   int initialY = 1;  // Whatever value it should have
   
   return new Player(defaultMaxEnergy, initialX, initialY);  
}

由於值不在您的描述中,我只是選擇了一個隨機數,但您可以選擇任何您想要的整數,這是有道理的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM