1.自定义一个 Fruit 类:该类有一个 类属性: identify,有两个对象属性: name,price,一个类方法: get_identify,一个对象方法:get_total_price(num):打印『%s个%s值%s钱』,一个静态方法:packing(*fruits)
静态方法(装箱)的思路分析
red_apple = Fruit(“红苹果”, 10)
green_apple = Fruit(“青苹果”, 10)
yellow_banana = Fruit(“黄香蕉”, 8)
调用:Fruit.packing(red_apple, green_apple, yellow_banana) 打印:一箱装了2个苹果1个香蕉
class Fruit:
identify = 'jeff水果公司'
def __init__(self, name, price):
self.name = name
self.price = price
@classmethod
def get_identify(cls):
return cls.identify
def get_total_price(self, num):
print("%s个%s值%s钱" % (num, self.name, str(self.price * int(num))))
@staticmethod
def packing(*fruits):
dic = {}
for i in fruits:
if len(i.name) == 3:
if i.name[1:3] not in dic: