简体   繁体   中英

Java - method that returns a class instance to different class?

I'm a beginner and have tried searching but nothing is coming up, apologies. I have 2 classes, I don't know how to link the rewardCoins (oneCoin/twoCoin/threeCoin) so that if called, it adds coins to the Bag inventory. The return is probably incorrect as well.

public class Bag {
    private int oneCoin;
    private int twoCoin;
    private int threeCoin;
    
    //etc
}

public class Thief {
     public Thief(String name, int level) {
        this.name = name;
        this.health = level * 100;
        this.level = level;
    }
    
    //problem code
    public Bag rewardCoins(){
        oneCoin =+ (int)(level-Math.random()*level/6);
        twoCoin =+ (int)(level-Math.random()*level/2);
        threeCoin =+ (int)((level-level/2)-Math.random()*(level-level/2));
        return rewardCoins();
    }
}

Not looking for immediate help, I just don't know what I should be reading up on to figure it out.

Edit: I may have explained it poorly - I can't use setters, as there are already things inside the Bag inventory. I want to add the coins to the inventory when a Thief is "defeated". Like dropping loot. I don't know how to access the private fields one/two/threeCoin in Thief to add them to the other class. They are 2 different classes, Thief.java and Bag.java

Second edit: for anyone reading this, the answer is changing the attributes and return value to-

public Bag rewardCoins(){
        int one = (int)(level-Math.random()*level/6);
        int two = (int)(level-Math.random()*level/2);
        int three = (int)((level-level/2)-Math.random()*(level-level/2));
        return (new Bag(one, two, three));
}

You may want something like this:

public class Thief {
    private final String name;
    private final int health;
    private final int level;

    public Thief(String name, int level) {
        this.name = name;
        this.health = level * 100;
        this.level = level;
    }

    public Bag rewardCoins(Bag bag){
        int oneCoin = bag.getOneCoin();
        int twoCoin = bag.getTwoCoin();
        int threeCoin = bag.getThreeCoin();

        oneCoin =+ (int)(level-Math.random()*level/6);
        twoCoin =+ (int)(level-Math.random()*level/2);
        threeCoin =+ (int)((level-level/2)-Math.random()*(level-level/2));

        Bag result = new Bag();
        result.setOneCoin(oneCoin);
        result.setTwoCoin(twoCoin);
        result.setThreeCoin(threeCoin);

        return result;
    }
}

or this:

public class Thief {
    private final String name;
    private final int health;
    private final int level;
    private Bag bag;

    public Thief(String name, int level) {
        this.name = name;
        this.health = level * 100;
        this.level = level;
    }

    public Bag rewardCoins(){
        int oneCoin = bag.getOneCoin();
        int twoCoin = bag.getTwoCoin();
        int threeCoin = bag.getThreeCoin();

        oneCoin =+ (int)(level-Math.random()*level/6);
        twoCoin =+ (int)(level-Math.random()*level/2);
        threeCoin =+ (int)((level-level/2)-Math.random()*(level-level/2));

        Bag result = new Bag();
        result.setOneCoin(oneCoin);
        result.setTwoCoin(twoCoin);
        result.setThreeCoin(threeCoin);

        return result;
    }
}

Well it depends. Looking at your code I can suppose that you want the thief to reward coins to the inventory which is shared among thieves. In that case you could have it as a variable of the thief and pass it as a parameter on the constructor. This way all thieves will be able to access the same Bag object.

public class Bag {
private int oneCoin;
private int twoCoin;
private int threeCoin;

//getters and setters
}

public class Thief {
private Bag bag;
 public Thief(String name, int level,final Bag bag) {
    this.name = name;
    this.health = level * 100;
    this.level = level;
    this.bag=bag;
}


public void rewardOneCoin(){

    bag.setOneCoin(bag.getOneCoin()+ (int)(level-Math.random()*level/6))
   
    
}

// Appropriate functions for two and three coins
}

If you can give me a bit more context I may cal provide you with a bit more help

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