车型类 温度类 购物车类
class Car:
def __init__(self,brand,speed,color,price):
self.b=brand
self.s=speed
self.c=color
self.p=price
def __repr__(self):
return '''\
brand:%s
speed:%s
color:%s
price:%s
''' % (self.b,self.s,self.c,self.p)
class CarInfo:
def __init__(self):
self.lst=[]
def addcar(self,car:Car):
self.lst.append(car)
def getall(self):
return self.lst
c=Car('bb',99,'red',999)
p=CarInfo()
p.addcar(c)
c=Car('vv',88,'blue',888)
p.addcar(c)
print(p.getall())
class Temperature:
def __init__(self,value:float,unit:str):
self.v=value
self.u=unit
def getc(self):
if self.u.upper() == 'C':
return str(value)+self.u
elif self.u.upper() == 'F':
return str(5*(self.v-32)/9)+'C'
else:
return str(self.v-273.15)+'C'
def getf(self):
if self.u.upper() == 'C':
return str(9*self.v/5+32)+'F'
elif self.u.upper() == 'F':
return str(self.v)+self.u
else:
return str(9*(k-273.15)/5+32)+'F'
def getk(self):
if self.u.upper() == 'C':
return str(self.v+273.15)+'K'
elif self.u.upper() == 'F':
return str((self.v-32)*5/9+273.15)+'K'
else:
return str(self.v)+self.u
b=Temperature(30,'c')
print(b.getf())
print(b.getk())
class Temperature:
def __init__(self,c=None,f=None,k=None):
self.c=c
self.f=f
self.k=k
def getk(self):
return
def getf(self):
return
def getc(self):
return
@classmethod
def c2f(cls,c):
return
@classmethod
def c2k(cls,c):
return
采用子类实现
class Celsius:
def __init__(self,v:float):
self.v=v
self.d={'f':self.c2f,'k':self.c2k}
def __repr__(self):
return '{}C'.format(self.v)
def c2f(self):
return Fahrenheit(self.v*9/5+32)
def c2k(self):
return Kelvin(self.v+273.15)
class Fahrenheit:
def __init__(self,v:float):
self.v=v
self.d={'c':self.f2c,'k':self.f2k}
def __repr__(self):
return '{}F'.format(self.v)
def f2c(self):
return Celsius((self.v-32)*5/9)
def f2k(self):
return Kelvin((self.v-32)*5/9+273.15)
class Kelvin:
def __init__(self,v:float):
self.v=v
self.d={'c':self.k2c,'f':self.k2f}
def __repr__(self):
return '{}K'.format(self.v)
def k2c(self):
return Celsius(self.v-273.15)
def k2f(self):
return Fahrenheit((self.v-273.15)*9/5+32)
class Temperature:
@classmethod
def convert(cls,temp,kind):
return temp.d[kind]()
b=Celsius(30)
print(Temperature.convert(b,'f'))
print(Temperature.convert(b,'k'))
class Temperature:
def __init__(self,v:float,unit:str='c'):
'''
class Temperature
:param v: float
:param unit: str
'''
self._c=None
self._f=None
self._k=None
if unit == 'c':
self._c=v
elif unit == 'f':
self._f=v
self._c=self.f2c(v)
else:
self._k=v
self._c=self.k2c(v)
@property
def c(self):
return self._c
@property
def f(self):
if self._f is None:
self._f=self.c2f(self._c)
return self._f
@property
def k(self):
if self._k is None:
self._k=self.c2k(self._c)
return self._k
@classmethod
def c2f(cls,v):
return 9*v/5+32
@classmethod
def c2k(cls,v):
return v+273.25
@classmethod
def f2c(cls,v):
return (v-32)*5/9
@classmethod
def f2k(cls,v):
return cls.c2k(cls.f2c(v))
@classmethod
def k2c(cls,v):
return v-273.15
@classmethod
def k2f(cls,v):
return cls.c2f(cls.k2c(v))
print(Temperature.c2f(30))
print(Temperature.c2k(30))
print(Temperature.f2c(86))
print(Temperature.k2c(303.15))
print(Temperature.k2f(303.15))
print(Temperature.f2k(86))
b=Temperature(30)
print(b.c,b.f,b.k)
bb=Temperature(303.15,'k')
print(bb.c,bb.f,bb.k)
class Color:
RED=0
BLUE=1
GREEN=2
GOLDEN=4
BLACK=8
OTHERS=100
class Item:
def __init__(self,**kwargs):
self.__spec=kwargs
def __repr__(self):
return str(sorted(self.__spec.items()))
class Cart:
def __init__(self):
self.items=[]
def additem(self,item:Item):
self.items.append(item)
def getallitems(self):
return self.items
cart=Cart()
phone=Item(brank='mm',color=Color.RED,memory='4g')
cart.additem(phone)
car=Item(brank='nn',color=Color.BLUE,year=2017)
cart.additem(car)
print(cart.getallitems())