关于python中的面向对象简单小应用,小明展示手中的牌

# 一些简单创建类以及属性构造,主要是练习面向对象的方法编写程序,没有注释,作为练习参考
class Poker:
    def __init__(self, color, number):
        self.color = color
        self.number = number

    def __str__(self):
        return f'{self.color}{self.number}'


poker1 = Poker('红桃', 'K')
poker2 = Poker('方片', 'A')


class Hand:
    def __init__(self, poker=None):
        self.poker = poker

    def hold_poker(self, poker):
        self.poker = poker


left_hand = Hand(poker1)
right_hand = Hand(poker2)


class Person:
    def __init__(self, left_hand, right_hand):
        self.left_hand = left_hand
        self.right_hand = right_hand
        self.name = '小宁'

    def show(self):
        print(f'{self.name}张开手')
        print(f'左手{self.left_hand.poker}')
        print(f'右手{self.right_hand.poker}')

    def sawp(self):
        print('小明交换两手的牌')
        self.right_hand.poker, self.left_hand.poker = self.left_hand.poker, self.right_hand.poker


xiaoming = Person(left_hand, right_hand)
xiaoming.show()
xiaoming.sawp()
xiaoming.show()

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值