简体   繁体   English

使用 java 中另一个 class 的构造函数在不同的类中创建对象

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

how can I use the constructor from another class in java to make an object through a method in separate class.我如何使用 java 中另一个 class 的构造函数通过单独的 ZA2F2ED4F8EBC2CBBDZC62A 中的方法制作 object。 For example below is a constructor in a player class例如下面是播放器 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;
    }

Which I want to use and create objects (player) through a method called我想通过一个名为的方法使用和创建对象(播放器)

private Player createPlayer() { 

and the above method is in separate class as并且上述方法在单独的 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.    

I have tried to initialize player within method with parameters and without parameters as**我试图用参数和不带参数的方法初始化播放器**

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

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

But it give errors.Any help will be appreciated.I am quite close to assume its not possible to have created objects like this.但它会给出错误。任何帮助将不胜感激。我非常接近假设不可能创建这样的对象。

below I share the complete game engine which is working with other classes as well.下面我分享了与其他类一起使用的完整游戏引擎。

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

Does something like this work for your case?这样的事情对你的情况有用吗?

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

Add a default no-args constructor in the player class.在播放器 class 中添加默认的无参数构造函数。 Once you create a constructor with Arg, java will not auto provide default one.一旦您使用 Arg 创建了构造函数,java 将不会自动提供默认值。

You have already declared Player你已经声明了 Player

private Player player;

So you must not try to reinitialize using same variable name, rather所以你不能尝试使用相同的变量名重新初始化,而是

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

} }

What is the error which you are facing?您面临的错误是什么? Can you share that?你能分享一下吗?

The following should do it:以下应该做到这一点:

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

Since the values are not in your descriptions I just selected a random number but you can pick whatever integers you want and that makes sense.由于值不在您的描述中,我只是选择了一个随机数,但您可以选择任何您想要的整数,这是有道理的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM