其他分享
首页 > 其他分享> > 使用面向对象的方法实现两张扑克牌的交换

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

作者:互联网

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();

    }
    
}

标签:poker,rightHand,扑克牌,Poker,两张,Hand,面向对象,leftHand,public
来源: https://blog.csdn.net/weixin_45511188/article/details/122813699