第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

構(gòu)造方法???

/**
?* 創(chuàng)建撲克牌類
?* @author?
?*
?*/
public class Card implements Comparable<Card> {

?? ?String num;
?? ?String type;
?? ?int ?rank; ? ?//rank代表等級(jí),值越大,等級(jí)越高 ? ? ? ?
?? ?public Card(String type,String num,int rank) {
?? ??? ?this.num=num;
?? ??? ?this.type=type;
?? ? ? ?this.rank=rank;
?? ?}?? ?
?? ?
?? ?public Card() {
?? ??? ?
?? ?}

?? ?@Override
?? ?public int compareTo(Card o) {
?? ??? ??
?? ??? ?return this.rank-o.rank; ? ? ?//比較rank值的大小
?? ?}


}


/**
?* 創(chuàng)建玩家類
?* @author?
?*
?*/
public class Player ?{

?? ?String id; ? ?
?? ?String name;
?? ?List<Card>pCards; ? //用于存放玩家手中的牌
?? ?
?? ?public Player(String id,String name) {
?? ??? ?this.id=id;
?? ??? ?this.name=name;
?? ? ? ?pCards=new ArrayList<Card>();
?? ?}

}


/**
?*?
?* @author?
?*
?*/
public class CardDemo {

?? ?List<Card>cards;
?? ?List<Player>players;
?? ?Player p1; ? ?//玩家
?? ?Player p2;
?? ?Card p1max; ? //玩家1最大的牌
? ? Card p2max; ? //玩家2最大的牌
?? ?Card p1min; ? //玩家1最小的牌
?? ?Card p2min; ? //玩家2最小的牌
//創(chuàng)建撲克牌資源數(shù)組
?? ?String[] CardType=new String[] {"方塊","梅花","紅桃","黑桃"}; ?//順序不可調(diào)換,否則會(huì)影響大小比較的rank值
?? ?String[] CardNum=new String[] {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
?? ??? ?
?? ?public CardDemo() {
?? ??? ?
?? ??? ?this.cards=new ArrayList<Card>();
?? ??? ?this.players=new ArrayList<Player>();
?? ?}?? ?
?? ?
//創(chuàng)建撲克牌?? ?
?? ?public void CreatCard() {
?? ??? ?
?? ??? ?System.out.println("---------創(chuàng)建撲克牌-----------");
?? ??? ?for(int i=0;i<13;i++) {
?? ??? ??? ?for(int j=0;j<4;j++) {
?? ??? ??? ? cards.add(new Card(CardType[j],CardNum[i],i*13+j));
?? ??? ??? ?}
?? ??? ?}
?? ??? ?//遍歷打印撲克牌資源
?? ??? ? System.out.println("--------撲克牌創(chuàng)建成功!----------");
?? ??? ? System.out.print("撲克牌為");
?? ??? ? for(Card c:cards) {?
?? ??? ??? ? System.out.print(c.type+c.num+",");?
?? ??? ? }
?? ??? ? System.out.println("總共52張");
?? ?}
?? ?
//進(jìn)行洗牌?? ?
?? ?public void washCard() {
?? ?System.out.println("----------開始洗牌--------------");?? ?
?? ??? ?Collections.shuffle(cards); ? ? ? ?//進(jìn)行洗牌
?? ?System.out.println("----------洗牌結(jié)束-------------");?? ?
?? ?
?? ?}

//創(chuàng)建兩名玩家
? ? ?public void creatPlayer() {
? ? ? ? //鍵盤上輸入兩名玩家信息 ?? ??
? ? ?? ?Scanner console=new Scanner(System.in);
? ? ? ? System.out.println("請(qǐng)輸入第一位玩家的ID和姓名:");
? ? ? ? System.out.println("請(qǐng)輸入ID");
? ? ?? ?String id1=console.next();?
? ? ?? ?System.out.println("請(qǐng)輸入姓名:");
? ? ?? ?String name1=console.next();
? ? ?? ?p1=new Player(id1, name1);
? ? ? ? players.add(p1);
? ? ? ? System.out.println("請(qǐng)輸入第二位玩家的ID和姓名:");
? ? ? ? System.out.println("請(qǐng)輸入ID");
? ? ?? ?String id2=console.next();?
? ? ?? ?System.out.println("請(qǐng)輸入姓名:");
? ? ?? ?String name2=console.next();
? ? ?? ?p2=new Player(id2, name2);
? ? ? ? players.add(p2);
? ? ?? ?System.out.println("歡迎玩家:"+p1.name);?
? ? ?? ?System.out.println("歡迎玩家:"+p2.name);?
? ? ?}
? ? ?
?//進(jìn)行發(fā)牌
? ? ?public void distrubiteCard() {
? ? ?? ?System.out.println("---------開始發(fā)牌------------");?
? ? ?? ?p1.pCards.add(cards.get(0));
? ? ? ? System.out.println("----玩家:"+p1.name+"-拿牌------");
? ? ? ? p2.pCards.add(cards.get(1));
? ? ? ? System.out.println("----玩家:"+p2.name+"-拿牌------");
? ? ? ? p1.pCards.add(cards.get(2));
? ? ? ? System.out.println("----玩家:"+p1.name+"-拿牌------");
? ? ? ? p2.pCards.add(cards.get(3));
? ? ? ? System.out.println("----玩家:"+p2.name+"-拿牌------");
? ? ?? ?System.out.println("---------發(fā)牌結(jié)束------------");?
? ? ?}
? ? ?
//開始游戲
? ? ?public void playGame() {
? ? System.out.println("--------開始游戲-----------");?? ?
? ? Collections.sort(p1.pCards); ? ? //對(duì)集合pCards進(jìn)行排序
? ? Collections.sort(p2.pCards);
? ? p1max=p1.pCards.get(1); ? ?
? ? p2max=p2.pCards.get(1);
? ? System.out.println(p1.name+"最大的手牌:"+p1max.type+p1max.num); ? //輸出玩家1的最大手牌
? ? System.out.println(p2.name+"最大的手牌:"+p2max.type+p2max.num); ? //輸出玩家2的最大手牌
? ? if(p1max.rank>p2max.rank)
? ? ?? ?{ ?System.out.println("-----玩家"+p1.name+"獲勝!-------"); ?}
? ? else?? ?
? ? ?? ?{ ?System.out.println("-----玩家"+p2.name+"獲勝!--------"); ?}
? ? //輸出玩家各自的手牌
? ? System.out.println("-------玩家各自的手牌為:----------");
? ? p1min=p1.pCards.get(0);
? ? p2min=p2.pCards.get(0);
? ? //打印兩名玩家各自手上的兩張牌
? ? System.out.println(p1.name+":"+p1max.type+p1max.num+","+p1min.type+p1min.num);
? ? System.out.println(p2.name+":"+p2max.type+p2max.num+","+p2min.type+p2min.num);
? ? System.out.println("--------游戲結(jié)束-----------");?? ? ?? ??
? ? ?}
? ? ?
//主函數(shù)?? ?
?? ?public static void main(String[] args) {
?? ??? ?CardDemo cd=new CardDemo();
? ? ? ? cd.CreatCard(); ? ? ? ? ?//創(chuàng)建撲克牌
? ? ? ? cd.washCard(); ? ? ? ? ? //洗牌
? ? ? ? cd.creatPlayer(); ? ? ? ?//創(chuàng)建玩家
? ? ? ? cd.distrubiteCard(); ? ? //進(jìn)行發(fā)牌
? ? ? ? cd.playGame(); ? ? ? ? ? //開始游戲
?? ?}

}


我不太明白為什么在CardDemo類中要重寫他的無參構(gòu)造方法。里面定義了兩個(gè)集合,這是干什么的?

就是這一段:?public CardDemo() {
?? ??? ?
?? ??? ?this.cards=new ArrayList<Card>();
?? ??? ?this.players=new ArrayList<Player>();
?? ?}???

正在回答

1 回答

構(gòu)造方法在構(gòu)造時(shí)構(gòu)造了一個(gè)對(duì)象,這個(gè)對(duì)象包含該類的所有屬性。但是List無法直接初始化,需要通過他的實(shí)現(xiàn)類ArrayList進(jìn)行實(shí)例化,所以在該無參構(gòu)造函數(shù)中需要對(duì)List進(jìn)行實(shí)例化?

我修改了一下代碼,在聲明List時(shí)直接就進(jìn)行了實(shí)例化,即List<Card>cards=new ArrayList();

List<Player>palyers=new ArrayList();

然后我把里面的無參構(gòu)造器注釋掉,發(fā)現(xiàn)程序可以正常運(yùn)行。

那他這個(gè)無參構(gòu)造器添加的這兩句其實(shí)是相當(dāng)于額外加上去的,這樣理解嗎?

0 回復(fù) 有任何疑惑可以回復(fù)我~

舉報(bào)

0/150
提交
取消

構(gòu)造方法???

我要回答 關(guān)注問題
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)