#面向对象三大特性
#1.继承
#2.封装
#3.多态
'''
1.继承的定义:是一种新建类的方式,新建的类称为子类,子类会遗传父类的属性,可以减少代码冗余。
在python中,子类(派生类)可以继承一个或者多个父类(基类,超类)
'''
#继承的写法
#class Parent1:
# pass
#class Parent2:
# pass
#class Sub1(Parent1):
# pass
#class Sub2(Parent1,Parent2):
# pass
#print(Parent1.__bases__) #查看子类继承了那些父类
#print(Sub1.__bases__) #查看子类继承了那些父类
#print(Sub2.__bases__) #查看子类继承了那些父类
#在python2中类分为2种:
#1.经典类,:指没有继承object类的类,以及该类的子类。
#2.新式类,:指继承object类的类,以及该类的子类。
#在python3中统一都为新式类:
#例
#class olstu:
# sch='oldboy'
# def __init__(self,name,age,sex):
# self.name=name
# self.age=age
# self.sex=sex
# def learn(self):
# print('%s is learing' %self.name)
#class olteach:
# sch='oldboy'
# def __init__(self,name,age,sex):
# self.name=name
# self.age=age
# self.sex=sex
# def tech(self):
# print('%s is teachring' % self.name)
#使用继承(效果同上)
class olper:
sch = 'oldboy'
def __init__(self, name, age, sex):
self.name = name
self.age = age
self.sex = sex
def tell__info(self):
print('名字:<%s> 年龄:<%s> 性别:<%s>' %(self.name,self.age,self.sex))
class olstu(olper):
def learn(self):
print('%s is learing' % self.name)
def tell__info(self):
print('wo is stu',end='')
print('名字:<%s> 年龄:<%s> 性别:<%s>' %(self.name,self.age,self.sex))
class olteach(olper):
def tech(self):
print('%s is teachring' % self.name)
#对象属相的查找顺序,先找在自己的名称空间找,找不到再去类的名称空间找,找不到再去父类的名称空间找。在哪儿找到,将不会去下个地方查找。
stu1=olstu('sut',18,'ser')
tea1=olteach('tea',18,'sse')
#print(stu1.__dict__)
#print(stu1.sch)
stu1.tell__info()
tea1.tell__info()
#属性查找
class foo:
def f1(self):
print('from foo.f1')
def f2(self): #self=obj
print('from foo.f2')
self.f1() #obj.f1()
class bar(foo):
def f1(self):
print('from bar.f1')
obj=bar()
obj.f2()
#子类重用父类的方法
#class olper:
# sch = 'oldboy'
# def __init__(self, name, age, sex):
# self.name = name
# self.age = age
# self.sex = sex
# def tell__info(self):
# print('名字:<%s> 年龄:<%s> 性别:<%s>' %(self.name,self.age,self.sex))
#class olstu(olper):
# def learn(self):
# print('%s is learing' % self.name)
# def tell__info(self):
# print('wo is stu',end='')
# olper.tell__info(self)
#stu1=olstu('sut',18,'ser')
#stu1.tell__info()
#
class olper:
sch = 'oldboy'
def __init__(self, name, age, sex):
self.name = name
self.age = age
self.sex = sex
def tell__info(self):
print('名字:<%s> 年龄:<%s> 性别:<%s>' %(self.name,self.age,self.sex))
class olstu(olper):
def __init__(self,name,age,sex,work):
#self.name=name
#self.age=age
#self.sex=sex
olper.__init__(self,name,age,sex)
self.work=work
def learn(self):
print('%s is learing' % self.name)
def tell__info(self):
print('wo is stu:',end='')
olper.tell__info(self)
stu1=olstu('sut',18,'ser','ss')
stu1.tell__info()
#子类重用父类的方法之super(),super安照继承原理进行查找。
class olper:
sch = 'oldboy'
def __init__(self, name, age, sex):
self.name = name
self.age = age
self.sex = sex
def tell__info(self):
print('名字:<%s> 年龄:<%s> 性别:<%s>' %(self.name,self.age,self.sex))
class olstu(olper):
def __init__(self,name,age,sex,work):
#self.name=name
#self.age=age
#self.sex=sex
super().__init__(name,age,sex)
self.work=work
def tell__info(self):
print('i is stu' ,end='')
# olper.tell__info(self)
super().tell__info()
stu1=olstu('hu',15,'sdf','python')
stu1.tell__info()
#super方法二
class Foo:
def f2(self):
print('=====')
def f1(self):
print('foo.f1')
super().f2()
class Bar:
def f2(self):
print('Bar.f2')
class Sub(Foo,Bar):
pass
s=Sub()
print(Sub.mro())
s.f1()
#组合
#class olper:
# sch = 'oldboy'
# def __init__(self, name, age, sex):
# self.name = name
# self.age = age
# self.sex = sex
# def tell__info(self):
# print('名字:<%s> 年龄:<%s> 性别:<%s>' %(self.name,self.age,self.sex))
#class olstu(olper):
# def __init__(self,name,age,sex,work,st_id,year,day):
# #self.name=name
# #self.age=age
# #self.sex=sex
# olper.__init__(self,name,age,sex)
# self.work=work
# self.st_id=st_id
# self.year=year
# self.day = day
# def learn(self):
# print('%s is learing' % self.name)
# def tell__info(self):
# print('wo is stu:',end='')
# olper.tell__info(self)
# def tell_birth(self):
# print('年日:《%s-%s》' %(self.year,self.day))
#class olteach(olper):
# def __init__(self,name,age,sex,level,salary,year,day):
# olper.__init__(self,name,age,sex)
# self.level=level
# self.salary=salary
# self.year=year
# self.day=day
# def tech(self):
# print('%s is teachring' % self.name)
# def tell_birth(self):
# print('年日:《%s-%s》' %(self.year,self.day))
# def tell__info(self):
# print('wo is teach:',end='')
# olper.tell__info(self)
#stu1=olstu('sut',18,'ser','ss',1,1995,9)
#teac1=olteach('tea',18,'tea',8,4000,1985,2)
#stu1.tell_birth()
#teac1.tell_birth()
#改写如下
class olper:
sch = 'oldboy'
def __init__(self, name, age, sex):
self.name = name
self.age = age
self.sex = sex
def tell__info(self):
print('名字:<%s> 年龄:<%s> 性别:<%s>' %(self.name,self.age,self.sex))
class olstu(olper):
def __init__(self,name,age,sex,work,st_id):
#self.name=name
#self.age=age
#self.sex=sex
olper.__init__(self,name,age,sex)
self.work=work
self.st_id=st_id
def learn(self):
print('%s is learing' % self.name)
def tell__info(self):
print('wo is stu:',end='')
olper.tell__info(self)
class olteach(olper):
def __init__(self,name,age,sex,level,salary):
olper.__init__(self,name,age,sex)
self.level=level
self.salary=salary
def tech(self):
print('%s is teachring' % self.name)
def tell__info(self):
print('wo is teach:',end='')
olper.tell__info(self)
class Date:
def __init__(self,year,day):
self.year=year
self.day=day
def tell_birth(self):
print('年日:《%s-%s》' % (self.year, self.day))
stu1=olstu('sut',18,'ser','ss',1)
date_obj1=Date(1995,9)
stu1.birth=date_obj1
teac1=olteach('tea',18,'tea',8,4000)
date_obj2=Date(1985,9)
teac1.birth=date_obj2
#print(stu1.birth)
#print(teac1.birth)
stu1.birth.tell_birth()
teac1.birth.tell_birth()
#组合的使用
#class olper:
# sch = 'oldboy'
# def __init__(self, name, age, sex, date_obj):
# self.name = name
# self.age = age
# self.sex = sex
# self.brith=date_obj
# def tell__info(self):
# print('名字:<%s> 年龄:<%s> 性别:<%s>' %(self.name,self.age,self.sex))
#class olstu(olper):
# def __init__(self,name,age,sex,work,st_id,date_obj):
#self.name=name
#self.age=age
#self.sex=sex
# olper.__init__(self,name,age,sex,date_obj)
# self.work=work
# self.st_id=st_id
# def learn(self):
# print('%s is learing' % self.name)
# def tell__info(self):
# print('wo is stu:',end='')
# olper.tell__info(self)
#class olteach(olper):
# def __init__(self,name,age,sex,level,salary,date_obj):
# olper.__init__(self,name,age,sex,date_obj)
# self.level=level
# self.salary=salary
# def tech(self):
# print('%s is teachring' % self.name)
# def tell__info(self):
# print('wo is teach:',end='')
# olper.tell__info(self)
#class oldxiao(olper):
# def __init__(self,name,age,sex,key,date_obj):
# olper.__init__(self,name,age,sex,date_obj)
# self.key=key
# def tell_info(self):
# print('xiaoshou',end='')
# olper.tell__info(self)
#class Date:
# def __init__(self,year,day):
# self.year=year
# self.day=day
# def tell_birth(self):
# print('年日:《%s-%s》' % (self.year, self.day))
#date_obj=Date(1995,5)
#salte1=oldxiao('可可',45,'salce',73,date_obj)
#salte1.brith=date_obj
#salte1.tell__info()
#salte1.brith.tell_birth()
class olper:
sch = 'oldboy'
def __init__(self, name, age, sex, date_obj):
self.name = name
self.age = age
self.sex = sex
self.brith=date_obj
def tell__info(self):
print('名字:<%s> 年龄:<%s> 性别:<%s>' %(self.name,self.age,self.sex))
class olstu(olper):
def __init__(self,name,age,sex,work,st_id,date_obj):
#self.name=name
#self.age=age
#self.sex=sex
olper.__init__(self,name,age,sex,date_obj)
self.work=work
self.st_id=st_id
def learn(self):
print('%s is learing' % self.name)
def tell__info(self):
print('wo is stu:',end='')
olper.tell__info(self)
class olteach(olper):
def __init__(self,name,age,sex,level,salary,date_obj):
olper.__init__(self,name,age,sex,date_obj)
self.level=level
self.salary=salary
self.caoure=[]
def tech(self):
print('%s is teachring' % self.name)
def tell__info(self):
print('wo is teach:',end='')
olper.tell__info(self)
class oldxiao(olper):
def __init__(self,name,age,sex,key,date_obj):
olper.__init__(self,name,age,sex,date_obj)
self.key=key
def tell_info(self):
print('xiaoshou',end='')
olper.tell__info(self)
class Date:
def __init__(self,year,day):
self.year=year
self.day=day
def tell_birth(self):
print('年日:《%s-%s》' % (self.year, self.day))
class caoure:
def __init__(self,name,precei,ptryird):
self.name=name
self.precei=precei
self.ptryird=ptryird
def tell_info(self):
print('详细信息:《%s,%s,%s》' %(self.name,self.precei,self.ptryird))
pytth=caoure('pythonsji',4566,'sf')
linux=caoure('linuxi',4566,'iii')
date_obj=Date(1998,9)
tech2=olteach('laohs',34,'ss',34,1000,date_obj)
tech2.caoure.append(pytth)
tech2.caoure.append(linux)
#print(tech2.caoure)
for i in tech2.caoure:
i.tell_info()
当前标题:面向对象三大特性
地址分享:
http://cdxtjz.com/article/psjgeh.html