斗地主原型:equals要重写.同时也要重写出:hashCode

学习常量:public static final 后要大写字母.



public class Card {


/**
* 打牌案例 :斗地主 toString():返回对象文本描述
*/
private int suit;// 花色
private int rank;// 点数
public static final int THREE = 0;// 从3开始,A和2最大.
public static final int FOUR = 1;
public static final int FIVE = 2;
public static final int SIX = 3;
public static final int SEVEN = 4;
public static final int EIGHT = 5;
public static final int NINE = 6;
public static final int TEN = 7;
public static final int JACK = 8;
public static final int QUEEN = 9;
public static final int KING = 10;// K
public static final int ACE = 11;// A
public static final int AEUCE = 12;// 2
public static final int BLACK = 13;// 小王
public static final int COLOR = 14;
/** 方块
*/
public static final int DIAMOND = 0;
/** 梅花
*/
public static final int CLUB = 1;
/**红桃*/
public static final int HEART = 2;
/**黑桃 */
public static final int SPADE = 3;
/**王 */
public static final int JOKER = 4;


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


public int getSuit() {
return suit;
}


public void setSuit(int suit) {
this.suit = suit;
}


public int getRank() {
return rank;
}


public void setRank(int rand) {
this.rank = rank;
}


public String toString() {
// suit =3 rank =11 算法转换:黑桃A
return suitName[suit] + rankName[rank];
}


private static String[] suitName = { "方块", "梅花", "红桃", "黑桃" };
private static String[] rankName = { "3", "4", "5", "6", "7", "8", "9",
"10", "J", "Q", "K", "A", "2", "小王", "大王" };

public boolean equals(Object obj){//重写equals父类
if(obj==null){return false;}

if(this==obj){//表示同一个对象
return true;
}
if(obj instanceof Card){
Card other = (Card)obj;
return this.rank==other.rank&&this.suit==other.suit;
}
return false;
}

public int hashCode(){//equals要重写.同时也要重写出:hashCode
return suit*100+rank;//c1(rank=12,suit=1) hashCode()=112   
}
}



public class Card {


/**
* 打牌案例 :斗地主 toString():返回对象文本描述
*/
private int suit;// 花色
private int rank;// 点数
public static final int THREE = 0;// 从3开始,A和2最大.
public static final int FOUR = 1;
public static final int FIVE = 2;
public static final int SIX = 3;
public static final int SEVEN = 4;
public static final int EIGHT = 5;
public static final int NINE = 6;
public static final int TEN = 7;
public static final int JACK = 8;
public static final int QUEEN = 9;
public static final int KING = 10;// K
public static final int ACE = 11;// A
public static final int AEUCE = 12;// 2
public static final int BLACK = 13;// 小王
public static final int COLOR = 14;
/** 方块
*/
public static final int DIAMOND = 0;
/** 梅花
*/
public static final int CLUB = 1;
/**红桃*/
public static final int HEART = 2;
/**黑桃 */
public static final int SPADE = 3;
/**王 */
public static final int JOKER = 4;


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


public int getSuit() {
return suit;
}


public void setSuit(int suit) {
this.suit = suit;
}


public int getRank() {
return rank;
}


public void setRank(int rand) {
this.rank = rank;
}


public String toString() {
// suit =3 rank =11 算法转换:黑桃A
return suitName[suit] + rankName[rank];
}


private static String[] suitName = { "方块", "梅花", "红桃", "黑桃" };
private static String[] rankName = { "3", "4", "5", "6", "7", "8", "9",
"10", "J", "Q", "K", "A", "2", "小王", "大王" };

public boolean equals(Object obj){//重写equals父类
if(obj==null){return false;}

if(this==obj){//表示同一个对象
return true;
}
if(obj instanceof Card){
Card other = (Card)obj;
return this.rank==other.rank&&this.suit==other.suit;
}
return false;
}

public int hashCode(){//equals要重写.同时也要重写出:hashCode
return suit*100+rank;//c1(rank=12,suit=1) hashCode()=112   
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值