简体   繁体   English

在Java中的两个类之间共享对象

[英]Sharing an object between two classes in Java

How do I share an object between two similar classes? 如何在两个相似的类之间共享对象? For example,I would like to have two players in a game: Human and Computer They will be sharing a Deck class which has a variable : ArrayList cardList The human and computer object needs to access the cardList at the same time to draw cards during the gameplay. 例如,我想在游戏中有两个玩家:人类和计算机他们将共享一个Deck类,该类具有一个变量:ArrayList cardList人类和计算机对象需要同时访问cardList才能在游戏玩法。

The cardList arraylist is passed to either the Human or Computer as an argument in the constructor to add cards to their own hand ArrayList.Is it possible to return the changed cardList arraylist after I have taken some cards to add to hand arraylist? 将cardList arraylist作为构造函数中的参数传递给人或计算机,以将卡添加到自己的手ArrayList中。在我将一些卡添加到手arraylist后,是否可以返回更改后的cardList arraylist?

Sorry if my explanation was confusing 抱歉,如果我的解释令人困惑

ArrayList is a mutable container. ArrayList是一个可变容器。 If you pass it to both objects on construction, any mutation that occurs on it will be reflected at any other reference. 如果将其传递给构造中的两个对象,则发生在其上的任何突变都将反映在任何其他参考上。 Basically what I'm saying is: Pass the ArrayList to both Objects, make changes in either object and the changes will be available in the other object and vise-versa. 基本上,我要说的是:将ArrayList传递给两个对象,在两个对象中进行更改,更改将在另一个对象中可用,反之亦然。

https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html

This is already the case. 已经是这样了。 If you have three classes Human, Computer and Deck. 如果您有人类,计算机和甲板三类。

Human : 人:

public class Human {
    private Deck commonDeck;
    private card currentCard;

    public Human(Deck deck) {
        commonDeck = deck;
    }

    public pickCard() {
        currentCard = commonDeck.removeLastCard();
    }
}

Computer: 电脑:

public class Computer {
    private Deck commonDeck;
    private card currentCard;

    public Computer(Deck deck) {
        commonDeck = deck;
    }

    public pickCard() {
        currentCard = commonDeck.removeLastCard();
    }
}

Deck: 甲板:

public class Deck {
    private List<Card> cards;

    public Deck(){
        cards = new ArrayList<Card>();
        /*populate the list*/
    }

    public Card removeLastCard() {
        return cards.remove(cards.size() - 1);
    }
}

Then when in your main you do : 然后在您的主要任务中执行以下操作:

public static void main() {
    Deck deck = new Deck();
    Human human = new Human(deck);
    Computer computer = new Computer(deck);
    //human and computer share the same deck object

    human.pickCard(); //human will remove a card from the list deck.cards
    //The deck object in computer is the same as in human
    //So coputer will see that a card has been removed
}

As explained in the code comments, human and computer objects share the same deck object. 如代码注释中所述,人员和计算机对象共享同一甲板对象。 They share the same reference to an instance of Deck. 它们共享对Deck实例的相同引用。 So whatever you do to deck in human, will be seen in computer. 因此,无论您做什么,都可以在计算机中看到。

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

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