gpt4 book ai didi

java - 不能在不覆盖的情况下对 child 使用父方法?

转载 作者:行者123 更新时间:2023-11-29 08:40:22 25 4
gpt4 key购买 nike

我有这个代码:

public class Cardmo {  

public static void main(String[] args) {

Decke poker = new Decke();

poker.shuffle();

PokerHand h1 = poker.deal();

// problem : it prints a hole new deck and not the PokerHand h1 ??
h1.print();
}
}

class Carte {

int suit, rank;

static String[] suits = { "Clubs", "Diamonds", "Hearts", "Spades" };
static String[] ranks = { "narf", "Ace", "2", "3", "4", "5", "6",
"7", "8", "9", "10", "Jack", "Queen", "King" };

public Carte() {
this.suit = 0;
this.rank = 0;
}

public Carte(int suit, int rank) {
this.suit = suit;
this.rank = rank;
}

public void print() {
System.out.println(ranks[rank] + " of " + suits[suit]);
}

public String toString() {
return ranks[rank] + " of " + suits[suit];
}
}

class Decke {

Carte[] cartes;

public Decke(int n) {
this.cartes = new Carte[n];
}

public Decke() {
this.cartes = new Carte[52];

int index = 0;
for (int suit = 0; suit <= 3; suit++) {
for (int rank = 1; rank <= 13; rank++) {
this.cartes[index] = new Carte(suit, rank);
index++;
}
}
}

public void print() {
for(int i = 0; i < cartes.length; i++) {
cartes[i].print();
}
}

public int randomInt(int low, int high) {
int x = (int)(Math.random() * (high-low) + low);
return x;
}

public void swapCartes(int i, int j) {
Carte t = cartes[i];
cartes[i] = cartes[j];
cartes[j] = t;
}

public void shuffle() {
for(int i =0; i < cartes.length; i ++) {
int swap = randomInt(i, cartes.length);
swapCartes(i, swap);
}
}

public Decke subDecke(int low, int high) {
Decke sub = new Decke(high - low + 1);

for(int i = 0; i < sub.cartes.length; i++) {
sub.cartes[i] = this.cartes[low + i];
}

return sub;
}

public PokerHand deal(int low) {
Decke hand = this.subDecke(low, low + 4);
PokerHand pokerhand = new PokerHand();

for(int i = 0; i < hand.cartes.length; i++) {
pokerhand.hand[i] = hand.cartes[i];
}

return pokerhand;
}
}

class PokerHand extends Decke {

Carte[] hand = new Carte[5];

public PokerHand(){}

//why override?? why not use the parent one ??
/* public void print(){
for(int i = 0; i < 5; i++) {
hand[i].print();
}
}*/
}

如您所见,我的问题是,每当我尝试使用父级的 print() 方法时,它都会打印一个 hole new deck 而不是手 h1。此外,每当我尝试使用父方法 h1.shuffle() 随机播放 h1 时,它都不会按预期工作。为什么?

如何正确使用父方法?

最佳答案

解决方案很简单:PokerHand 应该 扩展 Decke。这两个逻辑实体不满足“is-a”关系(也不满足“has-a”关系),因为它们都有截然不同的行为。使 PokerHand 成为自己的类,一个 Cards,另一个不扩展 Decke。如果您希望两个类共享方法,那么它们可能需要一个公共(public)抽象父类(我怀疑)或一个接口(interface)(也许)。

我还会对 Suits 和 Ranks 使用枚举,而不是打印方法,而是覆盖每个键类的 toString() 方法。这样它将像在 println 语句中一样工作。

此外,我会为您的等级和花色使用枚举(一种较新的 Java 常量类型)。这些是使用枚举的规范用例。像这样的东西:

enum Suit {
CLUBS("Clubs"), DIAMONDS("Diamonds"), HEARTS("Hearts"), SPADES("Spades");
private String name;

private Suit(String name) {
this.name = name;
}

public String getName() {
return name;
}

@Override
public String toString() {
return getName();
}
}

enum Rank {
ACE("Ace"), TWO("2"), THREE("3"), FOUR("4"), FIVE("5"), SIX("6"), SEVEN("7"), EIGHT("8"),
NINE("9"), TEN("10"), JACK("Jack"), QUEEN("Queen"), KING("King");

private String name;

private Rank(String name) {
this.name = name;
}

public String getName() {
return name;
}

@Override
public String toString() {
return getName();
}
}

然后 Card 将在“has-a”关系中使用上面的内容:

class Card {
private final Suit suit;
private final Rank rank;
public Card(Suit suit, Rank rank) {
this.suit = suit;
this.rank = rank;
}

public Suit getSuit() {
return suit;
}

public Rank getRank() {
return rank;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((rank == null) ? 0 : rank.hashCode());
result = prime * result + ((suit == null) ? 0 : suit.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Card other = (Card) obj;
if (rank != other.rank)
return false;
if (suit != other.suit)
return false;
return true;
}

@Override
public String toString() {
return "Card [suit=" + suit + ", rank=" + rank + "]";
}

}

您可能希望为 Card 提供一个 equals 和 hashCode 方法,以便您可以判断两个 Card 功能上是否相等。

然后 Deck 和 Hand 实际上可以共享一个父类(super class),比如说称为 CardCollection 的更深入的思考,我确实看到它们共享行为:

// super class for both Deck and Hand
abstract class CardCollection implements Iterable<Card> {
private List<Card> cards = new ArrayList<>();

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

public Card discardCard() {
// TODO: throw an exception if size is 0
return cards.remove(0);
}

public List<Card> getCards() {
return cards;
}

public int size() {
return cards.size();
}

@Override
public Iterator<Card> iterator() {
return cards.iterator();
}

@Override
public String toString() {
return "CardCollection [cards=" + cards + "]";
}

public void clear() {
cards.clear();
}

}

class Deck extends CardCollection {
public Deck() {
renew();
}

public void renew() {
clear();
for (Suit suit : Suit.values()) {
for (Rank rank : Rank.values()) {
addCard(new Card(suit, rank));
}
}
Collections.shuffle(getCards());
}

public Card deal() {
return discardCard();
}

}

class Hand extends CardCollection {

public static final int HAND_SIZE = 5;

}

关于java - 不能在不覆盖的情况下对 child 使用父方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40692459/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com