简体   繁体   English

从另一个类调用变量

[英]call a variable from another class

I'm working on a new project and I try to make a money system where I can withdraw it to transfer it to another variable (in another class).我正在做一个新项目,我尝试建立一个货币系统,在那里我可以提取它以将其转移到另一个变量(在另一个类中)。 I have a few problems solving this and I'm quite disappointed on how I could write the code for it.我在解决这个问题时遇到了一些问题,我对如何为其编写代码感到非常失望。 Here are the class that I want to link (I want to make "coinsamount" go into class 2 "amount" when I do /deposit这是我想要链接的课程(我想让“coinsamount”在我做 /deposit 时进入第 2 类“金额”

package Main;
import coins.bank;
import java.util.Random;
import java.util.Scanner;
public class Main {

    public void coinmanagement() {
        ///call class here 

    }



    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int coinsamount = 0;
        boolean mining;
        boolean playing;
        mining = false;
        playing = true;
        Random coins = new Random();
        System.out.println("Bot successfully started");






        while (playing) {
            String in = input.next();


            if (in.equals("/coins"))
                System.out.println("You have " + coinsamount + " coins!");

            if (in.equals("/mine")) {
                int minedcoins = coins.nextInt(200) + 1;
                coinsamount += minedcoins;
                System.out.println("You've mined " + minedcoins + " coins!");
            }

            if (in.equals("/deposit"))

                System.out.println("You sucessfully deposited" +    +  "coins");

            if (in.equals("/stop")) {
                break; }

            if (in.equals("/shop"))
                shop s = new shop();
            }
        }
    }

Class 2 :第 2 类:

package coins;
import Main.Main;
public class bank {
    private int amount = 0;

    public void add(int amount) {
        this.amount += amount;
    }
}

If i understand correctly when you get in the如果你进入时我理解正确

if (in.equals("/deposit"))

You want to deposit the coinsAmount into the Bank.您想将 coinAmount 存入银行。

To do that you should write your code as follows为此,您应该按如下方式编写代码

package Main;
import coins.bank;
import java.util.Random;
import java.util.Scanner;
public class Main {

    public void coinmanagement() {
        ///call class here 

    }



    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int coinsamount = 0;
        boolean mining;
        boolean playing;
        mining = false;
        playing = true;
        Random coins = new Random();
        System.out.println("Bot successfully started");
        Bank bank = new Bank();






        while (playing) {
            String in = input.next();


            if (in.equals("/coins"))
                System.out.println("You have " + coinsamount + " coins!");

            if (in.equals("/mine")) {
                int minedcoins = coins.nextInt(200) + 1;
                coinsamount += minedcoins;
                System.out.println("You've mined " + minedcoins + " coins!");
            }

            if (in.equals("/deposit"))
                bank.add(coinsamount);

                System.out.println("You sucessfully deposited" +    +  "coins");

            if (in.equals("/stop")) {
                break; }

            if (in.equals("/shop"))
                shop s = new shop();
            }
        }
    }

Make sure you have a constructor for your object确保你的对象有一个构造函数

package coins;
import Main.Main;
public class bank {
    private int amount = 0;

    public Bank() {
    }

    public void add(int amount) {
        this.amount += amount;
    }
}

What i did here was instantiate the Bank object in the main class just like you did with the Shop object in one of the if statements.我在这里所做的是在主类中实例化 Bank 对象,就像您在 if 语句之一中对 Shop 对象所做的一样。 If you want acces to members or methods/functions from a object you need to instantiate them in the class you want to use them.如果您想从对象访问成员或方法/函数,您需要在要使用它们的类中实例化它们。

The reason i did this at the top level of your scope in the main class is so that it's accessible everywhere in the main class.我在主类中的作用域的顶层执行此操作的原因是它可以在主类中的任何地方访问。 You can read about scopes here https://www.baeldung.com/java-variable-scope .您可以在https://www.baeldung.com/java-variable-scope阅读有关范围的信息。

As it is accessible within the scope i can call it anywhere and call on the bank.add() method.因为它可以在范围内访问,所以我可以在任何地方调用它并调用 bank.add() 方法。

Now if you want to see what you have stored into the amount variable in the Bank object you will need to set so called getters and setters.现在,如果您想查看存储在 Bank 对象中的数量变量中的内容,则需要设置所谓的 getter 和 setter。 https://www.codejava.net/coding/java-getter-and-setter-tutorial-from-basics-to-best-practices https://www.codejava.net/coding/java-getter-and-setter-tutorial-from-basics-to-best-practices

When you have those you can call a bank.getAmount() in your main class.当你有这些时,你可以在你的主类中调用bank.getAmount()

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

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