I have a question about the Java public method

Asked 2 years ago, Updated 2 years ago, 93 views

class Deck  {
final int CARD_NUM = 52; // Number of cards
CardcardArr[] = newCard[CARD_NUM]; // Include an array of Card objects

Deck () { // Initialize Deck's card.
    int i=0;

    for(int k=Card.KIND_MAX; k > 0; k--)
        for(int n=0; n < Card.NUM_MAX ; n++)
            cardArr[i++] = new Card(k, n+1);
}

Cardpick(intindex) { // Pull one card from the specified location (index) and return it
    return cardArr[index];
}

Select one card from Cardpick() { // Deck.
    int index = (int)(Math.random() * CARD_NUM);
    return pick(index);
}

Void shuffle() { // Mix the order of the cards.
    for(int i=0; i < cardArr.length; i++) {
        int r = (int)(Math.random() * CARD_NUM);

        Card temp = cardArr[i]; 
        cardArr[i] = cardArr[r];
        cardArr[r] = temp;
        }
    }
} // End of Deck Class

// Card Class
class Card {
Static final int KIND_MAX = 4; // Number of card patterns
Static final int NUM_MAX = 13; // Number of cards by pattern

static final int SPADE   = 4;
static final int DIAMOND = 3;
static final int HEART   = 2;
static final int CLOVER  = 1;

int kind;
int number;

Card() {
    this(SPADE, 1);
}

Card(int kind, int number) {
    this.kind = kind;
    this.number = number;
}

public String toString() {
    String[] kinds = {"", "CLOVER", "HEART", "DIAMOND", "SPADE"};
    String numbers = "0123456789XJQK"; // Number 10 expressed as X

    return "kind : " + kinds[this.kind] 
        + + ", number : " + numbers.charAt(this.number);
    } // End of toString()
} // End of Card Class

public class Tv {   
    public static void main(String args[]) {
        Deck d = new Deck(); // Create a deck of cards (Deck).
        Card c = d.Pick(0); // Pull out the top card before mixing.
        System.out.println(c); // System.out.println(c.toString());

        d.Shuffle(); // Mix the cards.
        c = d.After mixing pick(0); //, pull out the top card.
        System.out.println(c.toString());

    }

}

There is a method called public String to String() above the Tv class, and if you subtract public from this, you will get an error called cannot reduce visibility of the ingrained method from Object.

Can you tell me the reason? Please tell me the difference between public and public. Thank you.

java method

2022-09-20 19:42

1 Answers

It's a question about the access controller ^

^

https://88240.tistory.com/448


2022-09-20 19:42

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.