简体   繁体   English

Java - 将类实例返回到不同类的方法?

[英]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.我有 2 个课程,我不知道如何链接奖励硬币(oneCoin/twoCoin/threeCoin),以便如果调用,它会将硬币添加到 Bag 库存中。 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.编辑:我可能解释得不好——我不能使用二传手,因为 Bag 库存中已经有东西了。 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.我不知道如何访问 Thief 中的私有字段 one/two/threeCoin 以将它们添加到另一个类。 They are 2 different classes, Thief.java and Bag.java它们是 2 个不同的类,Thief.java 和 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.这样所有的窃贼都可以访问同一个 Bag 对象。

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如果你能给我更多的背景信息,我可能会为你提供更多帮助

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

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