简体   繁体   English

Java调用带有对象参数的方法到main方法

[英]Java calling a method with object parameters to main method

play.java: https://pastebin.com/4bzfE76z
values.java: https://pastebin.com/6mnUzKyA
player.java: https://pastebin.com/qiFymMF6
stats.java: https://pastebin.com/P24AUpXV

I have a method called start in play.java with object parameters. 我在play.java中有一个带有对象参数的名为start的方法。 The objects were originally declared in values.java inside the sValues method. 该物体在S值方法内values.java最初宣布。 I want to call the method start to the main method of play.java but I'm not sure what to write inside the parameters of the call. 我想将方法开始调用为play.java主要方法 ,但是我不确定在调用的参数中要写些什么。

Inside values.java : 内部values.java

    player user = new player ();
    user.setP_Name (username);
    user.setP_ATK (atk);
    user.setP_HP (hp);

    stats[] enemies = new stats [3];
    enemies [1] = new stats ();
    enemies [1] = gob;
    enemies [1].getName ();
    enemies [1].getHP ();
    enemies [1].getATK ();
    enemies [1].getMANA ();
    enemies [2] = new stats ();
    enemies [2] = orc;
    enemies [2].getName ();
    enemies [2].getHP ();
    enemies [2].getATK ();
    enemies [2].getMANA ();

Inside play.java , I use the objects like this: play.java内部,我使用如下对象:

System.out.println ("Hitpoints[HP]: " + enemies [i].getHP ());

and it works great because I included these two objects into the parameter of the method: 效果很好,因为我将这两个对象包含在方法的参数中:

public static void start (player user, stats[] enemies)

However, whenever I want to call start in the main method for it to do the things I want it to, it gives me errors. 但是,每当我要在main方法中调用start使其执行所需的操作时,都会出现错误。 This is how I've been trying to call it: 这就是我一直试图称呼它的方式:

public static void main (String[] args)
{ //main method
start(player, stats);
} //main method

Am I doing something wrong? 难道我做错了什么? Any help appreciated. 任何帮助表示赞赏。 I'll gladly add more information if needed. 如果需要,我会很乐意添加更多信息。 New to coding and not sure what specific details need to be provided. 编码新手,不确定需要提供哪些具体细节。

My errors: 我的错误:

{ //main method
    player player = new player();
    start(player, stats);
} //main method

Error on: start(player, stats ); 错误出现在:start(player, stats ); Cannot find symbol. 找不到标志。 Symbol: variable stats. 符号:可变统计信息。 Location: class play. 地点:上课玩。

This error is given when I hover over "stats" when using netbeans. 当我在使用netbeans时将鼠标悬停在“统计信息”上时,会出现此错误。

The main method is the entry point of your program. 主要方法是程序的入口点。 You have not instantiated anything else yet so there are no objects to pass into your start method. 您尚未实例化任何其他内容,因此没有对象可传递到start方法中。

You might need to give us more of your code, but you might need to try something like this first - and I this is making a lot of assumptions about your code 您可能需要给我们更多的代码,但是您可能需要先尝试类似的方法-而我正在对您的代码做出很多假设

public static void main (String[] args)
{ //main method
    Player player = new Player(); // This is an instance of your Player class
    // and initialize your stats array.
    Values v = new Values();
    start(player, stats);
} //main method

Also providing the error you are getting may help also. 同时提供您得到的错误也可能会有所帮助。

EDIT 编辑

After reviewing your code I can see some problems. 查看您的代码后,我可以看到一些问题。 In your Values class you have the method sValues which creates the enemies as you said, however they stats[] that you created in that method only has the scope of that method, so when that method is finished, the enemies you created have now disappeared. 在您的Values类中,您具有sValues方法,该方法可以按照您所说的创建敌人,但是您在该方法中创建的stats[]仅具有该方法的范围,因此当该方法完成时,您创建的敌人现在消失了。

You also have a lot of static reference - this is bad practice and you need to remove as much as possible. 您也有很多静态参考-这是不正确的做法,您需要尽可能多地删除它。 To do this you can change something like 为此,您可以更改类似

 Values.sValues();

to

 Values v = new Values(); // in the constructor call what you need to
 v.sValues();

You need to think about where you are going to store your enemies stats array. 您需要考虑将敌人统计数据存储在何处。 You could move it to the top of your Values class and add a getter for it then your code could look like this 您可以将其移至Values类的顶部,并为其添加一个getter方法,然后代码应如下所示

Values.java Values.java

public class values {
    public static String clear = "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
    public static String username;
    public static int atk = 1;
    public static int hp = 10;

    private stats[] enemies;

 // .. your other code
public void sValues () // this method should not be static
{ //sValue method 
    // .. other code
    enemies = new stats [3];  // Remove the declaration at the start of this line
    // .. other code
}

public stats[] getEnemies()
{
    return enemies;
}

Play.java 播放.java

public static void main (String[] args)
{
    // Create a player
    player player = new player("Steve", 10, 10);
    // Initialise your values
    Values v = new Values();
    // create the enemies (this could be done in your values constructor)
    v.sValues();
    // Start with these enemies
    start(player, v.getEnemies());
}

Seems to me you have to create an instance of values.java and then get the player and stats objects from there to pass on to the main method. 在我看来,您必须创建一个values.java实例,然后从那里获取player和stats对象,然后传递给main方法。 where are player, stats being declared in the play.java class? 在play.java类中声明播放器的状态在哪里?

try: 尝试:

{
  player player = new player();
  stats[] stats = new stats[5];

  start(player, stats);
} //main method

and initialize stats array with desired values 并用所需的值初始化stats数组

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

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