繁体   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