使用面向对象的方法实现两张扑克牌的交换

package Day08;

class Poker{
    //花色
    public String color;
    //大小
    public char nums;

    public Poker(){}

    public Poker(String color,char nums){
        this.color = color;
        this.nums = nums;
    }

}

class Hand{
    Poker poker = new Poker();
    String type;

    public Hand(){}

    public Hand(Poker poker,String type){
        this.poker = poker;
        this.type = type;
    }

    //设置一个拿牌的方法
    public Poker setPoker(Poker poker){
        this.poker = poker;
        return this.poker;
    }

}

class People{
    Hand leftHand;
    Hand rightHand;

    public People(){}

    public People(Hand leftHand,Hand rightHand){
        this.leftHand = leftHand;
        this.rightHand = rightHand;
    }

    //展示手中的牌
    public void showInfPoker(){
        System.out.println(leftHand.type+"手拿的牌的是"+leftHand.poker.color+leftHand.poker.nums);
        System.out.println(rightHand.type+"手拿的牌的是"+rightHand.poker.color+rightHand.poker.nums);
    }

    //交换手中的牌
    public void switchPoker(){
        //提取手中的牌
        Poker leftPoker = leftHand.poker;
        Poker rightPoker = rightHand.poker;

        leftHand.poker = leftHand.setPoker(rightPoker);
        rightHand.poker = rightHand.setPoker(leftPoker);
    }
}

public class SwitchPoker {

    public static void main(String[] args) {
        Poker pokerK = new Poker("红桃",'K');
        Poker pokerA = new Poker("黑桃",'A');
        Hand leftHand = new Hand(pokerA,"左");
        Hand rightHand = new Hand(pokerK,"右");
        People xiaoming = new People(leftHand,rightHand);
        xiaoming.showInfPoker();


        xiaoming.switchPoker();
        xiaoming.showInfPoker();

    }
    
}

上一篇:hands off mac破解版 mac受用户欢迎的一款防火墙工具


下一篇:10分钟玩转腾讯游戏核心数据库|TcaplusDB华东客户Hands-on活动