Learn Python 016: Coin - a project of msc, unfinished.

本文介绍了一个使用Python实现的硬币类设计,包括基本属性设置、稀有度判断、清洁与氧化处理方法及抛掷硬币等功能。通过继承实现了英镑硬币的具体类。
import random

class Coin:

    def __init__(self, rare=False, clean=True, **kwargs):

        for key,value in kwargs.items():
            setattr(self,key,value)

        self.is_rare = rare
        self.is_clean = clean
        self.heads = heads

        if self.is_rare:
            self.value = self.original_value * 1.25
        else:
            self.value = self.original_value * 1.0

        if self.is_clean:
            self.color = self.original_color
        else:
            self.color = self.rusted_color

    def rust(self):
        self.color = self.rusted_color

    def clean(self):
        self.color = self.original_color

    def flip(self):
        heads_options = [True, False]
        choice = random.choice(heads_options)
        self.heads = choice

    def __del__(self):
        print('Coin spent!')


class Pound(Coin):
    def __init__(self):
        data = {
            'original_value': 1.00,
            'original_color': 'gold',
            'rusted_color': 'greenish',
            'num_edge': 1,
            'diameter': 22.5,
            'thickness': 3.15,
            'mass': 9.5
        }
        super().__init__(**data)



    # def __init__(self, rare=False):
    #     self.rare = rare
    #     if self.rare:
    #         self.value = 1.25
    #     else:
    #         self.value = 1.00
    #     self.value = 1.00
    #     self.color = 'gold'
    #     self.num_edge = 1
    #     self.diameter = 22.5
    #     self.thickness = 3.15
    #     self.heads = True
    #
    # def rust(self):
    #     self.color = 'greenish'
    #
    # def clean(self):
    #     self.color = 'gold'
    #
    # def flip(self):
    #     heads_options = [True, False]
    #     choice = random.choice(heads_options)
    #     self.heads = choice
    #
    # def __del__(self):
    #     print('Coin spent!')

 

转载于:https://www.cnblogs.com/mxyzptlk/p/7203267.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值