类的Java对象未返回相同的值 [英] Java objects of classes not returning the same values

查看:70
本文介绍了类的Java对象未返回相同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从2个单独的类中创建一个类的对象,并且两个对象都针对同一方法返回不同的值。我怀疑这可能与while循环有关,但这里是类。主要类起作用,设置类是将变为对象的类,而游戏循环类的对象未返回正确的值。

I am creating an object of a class from 2 separate classes and both objects are returning different values for the same method. I suspect it may be an issue with the while loop but here are the classes. The main class works, the setup class is the class that is being turned into and object and the game loop class has the object that doesn't return the right values. it returns the values defined at the beginning of setup and not the modified versions.

import java.util.Scanner;

public class MainClass {
    static Scanner input = new Scanner(System.in);
    //String x = input.nextLine();

    public static void main(String[] args)
    {
        setup setupGov = new setup();
        gameLoop gameLoop = new gameLoop();

        setupGov.statsSetup();
        System.out.println("happyness: " + setupGov.getHappyness() + " money: £" + setupGov.getMoney() + " population: " + setupGov.getPopulation());

        gameLoop.loop();

    }
}







import java.util.Scanner;

public class setup {
    static Scanner input = new Scanner(System.in);

    String goverment;
    int happyness;
    double money;
    int population = 1000000;


    public setup()
    {
    }

    public void statsSetup()
    {
        System.out.println("Choose a goverment: 1. democracy 2. monarchy 3. dictatorship");
        goverment = input.nextLine();

        if (goverment.equals("1"))
        {
            happyness = 75;
            money = 250000.0;

        }
        else if (goverment.equals("2"))
        {
            happyness = 50;
            money = 500000.0;
        }
        else if (goverment.equals("3"))
        {
            happyness = 25;
            money = 750000.0;
        }
        else
        {
            System.out.println("ENTER A VALID VALUE");
        }
    }
    public int getHappyness()
    {
        return happyness;
    }
    public double getMoney()
    {
        return money;
    }
    public int getPopulation()
    {
        return population;
    }
}







import java.util.Scanner;

public class gameLoop 
{
    static Scanner input = new Scanner(System.in);

    static int turn = 0;
    int happyness;
    double money;
    int population;

    public gameLoop()
    {
    }

    setup setupGov = new setup();

    public static void main(String[] args)
    {

    }

    public void loop() 
    {
        while (true)
        {
            System.out.println("Turn: "+turn);
            input.nextLine();
            turn++;
        }
    }

}


推荐答案

您要创建两个不同的类设置实例。一个直接在主函数中创建,另一个在gameLoop对象中创建。它们不共享其属性,因此方法可能返回不同的值。每次您使用 new运算符时,都会使用其自己的属性创建一个新的类实例(由于静态成员属于类而不是实例,因此仅共享静态成员)。如果要使用相同的实例,则可以编写:

You are creating two different instances of class setup. One is created directly in main function and other is created in gameLoop object. They do not share their attributes so methods may return different value. Every time you use 'new' operator, a new instance of class is created with it's own attributes (only static member are shared since static member belongs to class instead of instances). If you want to have same instances you could write:

 public class gameLoop 
 {
    static Scanner input = new Scanner(System.in);

    static int turn = 0;
    int happyness;
    double money;
    int population;

    public gameLoop(setup setupGov) 
    {
       this.setupGov = setupGov;
    }

    setup setupGov;

    public static void main(String[] args)
    {

    }

    public void loop() 
    {
        while (true)
        {
            System.out.println("Turn: "+turn);
            input.nextLine();
            turn++;
        }
    }

}

main:

public class MainClass {
    static Scanner input = new Scanner(System.in);
    //String x = input.nextLine();

    public static void main(String[] args)
    {
        setup setupGov = new setup();
        gameLoop gameLoop = new gameLoop(setupGov);

        setupGov.statsSetup();
        System.out.println("happyness: " + setupGov.getHappyness() + " money: £" + setupGov.getMoney() + " population: " + setupGov.getPopulation());

        gameLoop.loop();

    }
}

现在两个对象setupGov都将是

Now both of objects setupGov will be the same instance.

请注意:
最好用大写的第一个字母写类名。 GameLoop而不是gameLoop

Please note: It is good practice to have class name written with capitalized first letter eg. GameLoop instead of gameLoop

这篇关于类的Java对象未返回相同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆