简体   繁体   中英

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

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 :

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.

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. 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 .

As it is accessible within the scope i can call it anywhere and call on the bank.add() method.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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