类有三个属性:封装;继承;多态
继承: 子类继承父类,可以使用父类的方法
抽象: 约束所有子类,实现被 abstractmethod装饰的方法名给我们的代码指定规范
多态:
抽象类
from abc import ABCMeta,abstrectmethod
class Payment(metaclass = ABCMeta):
@abstractmethod
def pay(self):
pass
class Wechatpay(Payment):
def __init__(self,name,money):
self.name = name
self.money = money
def pay(self):
print('%s通过微信支付了%s元'%(self.name,self.money))
def back(self):
pass
ping = Wechatpay('李强',3000)
ping.pay()
class Student:
def __init__(self,name,num,course):
self.name = name
self.num = num
self.course = course
class Course:
def __init__(self,name,price,per):
self.name = name
self.price = price
self.per = per
python = Course('python',30000,'6 month')
s1 = Student('haha1',100000,python)
s2 = Student('haha2',100001,python)
python.price = 40000
print(s1.course.price)
class Date:
def __init__(self,year,month,day):
self.year = year
self.month = month
self.day = day
def date(self):
return '%s%s%s'%(self.year,self.month,self.day)
class Student:
def __init__(self,name,num,birth,in_school,start_dat):
self.name = name
self.num = num
self.birth = birth
self.in_school = in_school
self.start_dat = start_dat
d1 = Date(1999,10,23)
d2 = Date(1989,10,21)
d3 = Date(1989,10,21)
feng = Student('冯磊',10020,d1,d2,d3)
print(feng.birth.year)
print(feng.start_dat.month)
print(feng.birth.year)
feng.start_dat.month += 1
print(feng.start_dat.date())