简体   繁体   English

独立程序中的公共变量

[英]Public Variables in a Stand-Alone Program

I'm writing a small Java program that randomly deals out playing cards, then displays them on screen in a window. 我正在编写一个小的Java程序,该程序随机发出纸牌,然后在窗口的屏幕上显示它们。

Since I'm using NetBeans, the GUI was started for me, and I've been writing my methods for randomly choosing cards, setting up an array to store whether or not a card has already been dealt, etc., all in the same class NetBeans set up for me when it built the JFrame. 自从我使用NetBeans以来,GUI就是为我启动的,并且我一直在编写用于随机选择纸牌,设置数组以存储是否已处理纸牌的方法,等等。 NetBeans类在构建JFrame时为我设置。

I'd like to move all my non-GUI code into its own Class, and then just pass data back to the GUI class as needed to display the cards, but I'm not sure of the best way to share data between the two classes. 我想将所有非GUI代码移动到其自己的类中,然后根据需要将数据传递回GUI类以显示卡,但是我不确定在两者之间共享数据的最佳方法类。

I know about set/get methods and I know I could make public class-level variables, but everything I've been reading tells me to avoid both as much as possible. 我了解set / get方法,并且知道可以创建公共类级别的变量,但是我一直在阅读的所有内容都告诉我尽可能避免两者。

Right now I have a method that generates an int between 1 and 52 for each card dealt. 现在,我有一种方法可以为每张发牌生成1到52之间的整数。 1 = Ace of spades, 2= 2 of spades, etc. Once the GUI has that number, it can display the appropriate card in the appropriate place on the screen (or at least it will be able to once I've coded the GUI side of things). 1 =黑桃A,2 = 2黑桃,依此类推。一旦GUI有了该数字,它就可以在屏幕上的适当位置显示适当的卡(或者至少在我对GUI进行编码后就可以了)方面)。 If I'm looking to pass that integer value to the GUI class, then display a specific card on the screen based on that value, how should I do it? 如果我想将该整数值传递给GUI类,然后根据该值在屏幕上显示特定的卡,该怎么办?

Seems to me a public variable would make this easy, as would a simple get method...but in the interest of avoiding those options is there another way? 在我看来,像使用简单的get方法那样,使用公共变量将使此操作变得容易……但是为了避免这些选择,还有另一种方法吗?

I can provide code snippets if that helps. 如果有帮助,我可以提供代码段。

Here's one way you could start to implement this idea using OO concepts. 这是您可以开始使用OO概念来实现此想法的一种方法。

Make a Card class to represent a card. 制作一个Card类来代表一张卡。

public class Card {
    // FIELDS
    // card name
    private final String name;
    // card value (number)
    private final int value;
    // another arbitrary value to demonstrate setter
    private Object arbitraryValue;


    public Card(String name, String value) {
        this.name = name;
        this.value = value;
    }

    public String getName() {
        return this.name;
    }

    public int getValue() {
        return this.value;
    }

    public Object getArbitraryValue() {
        return this.arbitraryValue;
    }

    public void setArbitraryValue(Object arbitraryValue) {
        this.arbitraryValue = arbitraryValue;
    }
}

Create a CardManager class to hold methods that pertain to handling cards (eg utility methods and card data storage) 创建一个CardManager类来保存与处理卡有关的方法(例如,实用程序方法和卡数据存储)

public class CardManager() {
    private List<Card> cards = new ArrayList<Card>();

    public void addCard(Card card) {
        this.cards.add(card);
    }

    // and so on...your methods here.
}

Finally, create a class for your GUI ( CardGUI ) management and make use of the other classes to manage it. 最后,为您的GUI( CardGUI )管理创建一个类,并利用其他类进行管理。

You can do so like this: 您可以这样做:

public class CardGUI() {
    public static void main(String[] args) {
        // create your GUI and put your logic here...
        // also use your other classes, perhaps like so.
        Card card = new Card("One", 1);
        CardManager cardManager = new CardManager();
        cardManager.addCard(card);

        // From there you can manage your cards through other classes.
}

Hope this helps / demonstrates how to share data between classes following standards. 希望这有助于/演示如何遵循标准在类之间共享数据。

Edit: To answer your question on exactly how you would get the value, then see the above Card class. 编辑:要确切回答您将如何获取值的问题,请参阅上述Card类。 You would simple create a new card ( Card card = new Card("name", intval); ), then you would use the method Card#getValue() to get that value and display it in the GUI. 您可以简单地创建一个新卡( Card card = new Card("name", intval); ),然后使用Card#getValue()方法获取该值并将其显示在GUI中。

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

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