设为首页 加入收藏

TOP

pyhton 面向对象之 小明左右手换牌
2019-09-17 17:45:27 】 浏览:27
Tags:pyhton 面向 对象 小明 左右手

'''
#左右手交换牌 案列
#小明手里有俩张牌,左手红桃?K,右手黑桃?A,小明交换俩手的牌后,手里分别是什么?

人类:
    属性:小明,左手,右手
    行为:展示手里的牌, 交换手里的牌
手类:
    属性:牌
    行为:手里拿着扑克牌
牌类:
    属性:颜色,数字(红桃?K ,黑桃?A)


思路:
    先找到对象:左手,右手,?K,黑桃A,小明
    根据对象抽出对应的类:人,手,牌
    根据需要写出相应地逻辑,很可能反过来完善类的设计
    按照题目要求创建相关对象,调用相关方法,实现相关功能


'''

 

   #扑克类
    class Poker:
    
        def __init__(self,color,num):
            self.color = color
            self.num = num
    
        def __str__(self):
            return '{}{}'.format(self.color,self.num)
    #创建牌类对象
    p1 = Poker('?','K')
    p2 = Poker('?','A')
    
    
    
    #手类
    class Hond:
        def __init__(self,poker):
            self.poker = poker
    
        def hold_poker(self,poker):
            self.poker = poker
    
    #创建左右手对象
    life_hond = Hond(p1)
    right_hond = Hond(p2)
    # print(life_hond)
    # print(right_hond)
    
    #人类
    class Person:
    
        def __init__(self,name,life_fond,right_fond):
            self.name = name
            self.life_fond = life_fond
            self.right_fond = right_fond
    
        #展示手里的牌
        def show(self):
            print('{}张开双手'.format(self.name),end='')
            print('左手:{}'.format(self.life_fond.poker),end=',')
            print('右手:{}'.format(self.right_fond.poker))
    
        #交换手里的牌
        def swap(self):
            self.life_fond.poker,self.right_fond.poker = self.right_fond.poker,self.life_fond.poker
            print('{}交换俩手的牌'.format(self.name))
    
    #创建小明对象
    xiaoming = Person('小明',life_hond,right_hond)
    
    #展示手里的牌
    xiaoming.show()
    
    #交换手里的牌
    
    xiaoming.swap()
    
    #在展示手里的牌
    
    xiaoming.show()

 








】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Java Activiti 流程审批 后台框架.. 下一篇HRMS(人力资源管理系统)-SaaS架构..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目