Java 卡片不是抽象错误吗?
我创建了一个程序,可以扩展给定的类。然而,我得到了一个错误,指出程序不是抽象的,而无论如何它都不应该是抽象的 这是我试图解决的问题: 一副标准的扑克牌由52张牌组成。每张牌都有一个等级和一套衣服。您将创建一个名为Card的类,该类将模拟标准卡组中的卡。您的类将扩展AbstractCard类(提供) 标准牌组中的牌的顺序(如本任务所定义)首先由套牌指定,如果套牌相同,则由等级指定 西服和军衔的顺序如下: 西服:西服将被订购 钻石<梅花<红心<黑桃 等级:将对等级进行排序 2<3<··<9<10Java 卡片不是抽象错误吗?,java,debugging,inheritance,abstract-class,Java,Debugging,Inheritance,Abstract Class,我创建了一个程序,可以扩展给定的类。然而,我得到了一个错误,指出程序不是抽象的,而无论如何它都不应该是抽象的 这是我试图解决的问题: 一副标准的扑克牌由52张牌组成。每张牌都有一个等级和一套衣服。您将创建一个名为Card的类,该类将模拟标准卡组中的卡。您的类将扩展AbstractCard类(提供) 标准牌组中的牌的顺序(如本任务所定义)首先由套牌指定,如果套牌相同,则由等级指定 西服和军衔的顺序如下: 西服:西服将被订购 钻石
public abstract class AbstractCard implements Comparable<AbstractCard>{
/* handy arrays for ranks and suits */
/* do NOT change these */
public final static String[] RANKS = { "None", "None",
"2", "3", "4", "5", "6", "7", "8", "9", "10",
"Jack", "Queen", "King", "Ace", "Joker"};
public final static String[] SUITS = { "Diamonds", "Clubs", "Hearts", "Spades", "None"};
/** the numerical representation of the rank of the current card
* <p>
* ranks have the numerical values
* 2 = 2, 3 = 3, ..., 10 = 10
* Jack = 11, Queen = 12, King = 13, Ace = 14
* Joker = 1
* @return the numerical rank of this card
*/
public abstract int getRank();
/** the string representation of the rank of the current card
*
* @return the string representation of the rank of this card
* (must be a string from Card.RANKS)
*/
public abstract String getRankString();
/** the suit of the current card
*
* @return the suit of this card (must be a string from Card.SUITS)
*/
public abstract String getSuit();
@Override
public final String toString(){
// outputs a string representation of a card object
int r = getRank();
if( r >= 2 && r <= 14 ){
return r + getSuit().substring(0,1);
}else if (r == 1){
return "J";
}
return "invalid card";
}
}
public class Card extends AbstractCard {
public Card(String rank,String suit) {
this.strRank = rank;
this.suit = suit;
setIntRank();
}
public Card(int rank,String suit) {
this.intRank = rank;
this.suit = suit;
setStringRank();
}
private void setIntRank() {
String temp = strRank.toLowerCase();
switch(temp) {
case "jack":
intRank = 11;
break;
case "queen":
intRank = 12;
break;
case "king":
intRank = 13;
break;
case "ace":
intRank = 14;
break;
default:
intRank = Integer.parseInt(strRank);
break;
}
}
private void setStringRank() {
switch(intRank) {
case 11:
strRank="Jack";
break;
case 12:
strRank="Queen";
break;
case 13:
strRank="King";
break;
case 14:
strRank="Ace";
break;
default:
strRank = ""+intRank;
break;
}
}
public int getRank() {
return intRank;
}
public String getRankString() {
return strRank;
}
public String getSuit() {
return suit;
}
}
您需要实现由
Compariable
接口
定义的功能,而AbstractCard
承诺遵守该接口
这意味着将方法compareTo
添加到AbstractCard
(可能更好)或Card
@Override
public int compareTo(AbstractCard o) {
// Your implementation
}
我建议从
接口开始,它将描述卡
承诺提供的功能。然后,我将使用抽象类来实现核心或公共功能(如toString
和compareTo
),然后进行具体实现。主要原因是,它将代码解耦到更高的程度,并允许在不影响依赖于接口的代码的情况下更改实现,但这只是我;) 我不认为Card
实现了compare
中的compare
方法,我们不允许更改AbstractCard,如果将该方法添加到Card中会有很大的不同吗?@MuthuShank这意味着您必须在任何其他类中重新实现它(除了Card
)这扩展了AbstractCard,这可能是浪费时间。如果您不打算编写任何其他这样的类,那么它应该不会有什么不同。@MuthuShank这并不是抽象应该如何工作,但是如果您在从AbstractCard
扩展的每个类中都要实现抽象,那么您可以这样做,但是,这可能会导致问题,因为这是一个潜在的错误点(由于compareTo
的一个实现可能与另一个不同-无意中)。有什么问题吗?没有,我会这样做吗?没有可能在我应该做什么方面得到任何帮助吗?完全不知道我必须做什么才能使这项工作符合问题的要求。@MuthuShank您必须实现compareTo
方法。它需要确定当前obj的天气状况ect大于、小于或等于提供的对象