简体   繁体   中英

How to create instance of Card class in Java

I'm new to Java and I'm kind of lost on how classes work syntactically. I have the following class:

import java.util.ArrayList;
import java.util.List;

public class Card {

    public final Face face;
    public final Suit suit;
    private boolean visible;

    public enum Face {
    TWO(2), THREE(3), FOUR(4), FIVE(5), SIX(6), SEVEN(7), EIGHT(8), NINE(
                                         9), TEN(10), JACK(10), QUEEN(10), KING(10), ACE(11);

    private int value;

    Face(int value) {
        this.value = value;
    }

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

    public enum Suit {
    CLUB, DIAMOND, HEART, SPADE;
    }

    public Card(Face face, Suit suit){
    this.face = face;
    this.suit = suit;

    }

    @Override
    public boolean equals(Object o){
    if(!(o instanceof Card)) return false;
    if(this == o) return true;

    return this.toString().equals(o.toString());
    }



    public String toString(){
    return this.face.name() + this.suit.name();

    }

    private static final List<Card> protoDeck = new ArrayList<Card>();
    static {
    for (Suit suit : Suit.values())
        for (Face face : Face.values())
        protoDeck.add(new Card(face, suit));
    }

    public static ArrayList<Card> newDeck() {
    return new ArrayList<Card>(protoDeck); // Return copy of prototype deck
    }

    public boolean isVisible() {
    return this.visible;
    }

    public void setVisible(boolean b){
    this.visible = b;
    }

}

How can I create an instance of this class? For example how would I create a Card that is the 5 of hearts?

像这样:

 new Card(Card.Face.FIVE, Card.Suit.HEART)

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