public enum Suit {
CLUBS, DIAMONDS, HEARTS, SPADES
}
public class PlayingCard { private Suit suit; private int number;
public PlayingCard(Suit suit, int number) { this.suit = suit; this.number = number; }
public String getString() { String numberString = switch (number) { case 11 -> "Jack"; case 12 -> "Queen"; case 13 -> "King"; case 1 -> "Ace"; default -> Integer.toString(number); };
return numberString + " of " + suit; }
public Suit getSuit() { return suit; }
public void setSuit(Suit suit) { this.suit = suit; }
public int getNumber() { return number; }
public void setNumber(int number) { this.number = number; }
}
public class Main {
public static void main(String[] args) { var card1 = new PlayingCard(Suit.SPADES, 13); var card2 = new PlayingCard(Suit.HEARTS, 1); var card3 = new PlayingCard(Suit.DIAMONDS, 3);
public class PlayingCardPlus extends PlayingCard {
public static int count = 0;
public PlayingCardPlus(Suit suit, int number) {
super(suit, number);
count++;
}
}
public class Main {
public static void main(String[] args) {
var card1 = new PlayingCardPlus(Suit.SPADES, 13);
var card2 = new PlayingCardPlus(Suit.HEARTS, 1);
var card3 = new PlayingCardPlus(Suit.DIAMONDS, 3);
System.out.println(card1.getString());
System.out.println(card2.getString());
System.out.println(card3.getString());
System.out.println("Number of cards: " + PlayingCardPlus.count);
}
}
The first println displays the static name
variable belonging to the Main1 class; the second println
displays the name variable that's local to the main
method.
The first println displays the name
variable that's local to the OtherClass constructor.
The second println displays that static name
field that belongs to the Main3 class.
The first println refers to the name
variable that's local to the YetAnotherClass
constructor. The second println refers to the name
field belonging to the whoCreatedMe object (an
instance of the Main4 class).