码迷,mamicode.com
首页 > 编程语言 > 详细

python面向对象编程

时间:2018-10-23 23:10:50      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:course   teacher   people   对象   pre   ase   顺序   解决   eve   

       继承介绍:

 

 

class Parent1(object):
pass

class Parent2:
pass

class Sub1(Parent1):
pass

class Sub2(Parent1,Parent2):
pass

print(Sub1.__bases__)
print(Sub2.__bases__)

print(Parent1.__bases__)
print(Parent2.__bases__)





class Foo:
x=1
pass
obj=Foo()
obj.x=3
print(obj.x)



继承解决类与类代码冗余的问题:


class OldboyPeople:
school = ‘Oldboy‘
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender


print(OldboyPeople.__init__)


class OldboyStudent(OldboyPeople):
def __init__(self,name,age,gender):
self.name = name
self.age = age
self.gender = gender


def choose_course(self):
print(‘%s is choosing course‘ %self.name)


class OldboyTeacher(OldboyPeople):
def __init__(self,name,age,gender,level,salary):
self.name = name
self.age = age
self.gender = gender
OldboyPeople.__init__(self,name,age,gender)

self.level=level
self.salary=salary

def score(self,stu,num):
stu.num=num
print(‘老师%s给学生%s打分%s‘ %(self.name,stu.name,num))

stu=OldboyStudent(‘kevin‘,38,‘male‘) #__init___(stu1,‘kevin‘,38,‘male‘)
print(stu.__dict__)
tea=OldboyTeacher(‘egon‘,18,‘male‘,10,3000) #__init___(tea,‘egon‘,18,‘male‘,10,3000)
print(tea.__dict__)
print(stu.school)
print(tea.school)


单继承背景下属性的查找顺序:

class Foo:
def f1(self):
print(‘Foo.f1‘)

def f2(self):
print(‘Foo.f2‘)
self.f1() #obj.f1()

class Bar(Foo):
def f1(self):
print(‘Bar.f1‘)

obj=Bar()
obj.f2()


class E:
xxx=‘E‘
pass

class F:
xxx=‘F‘
pass

class B(E):
xxx=‘B‘
pass

class C(F):
xxx=‘C‘
pass

class D:
xxx=‘D‘
pass

class A(B,C,D):
xxx=‘A‘
pass

# obj=A()
# obj.xxx=111
# print(obj.xxx)

print(A.mro())


class G:
xxx=‘G‘

class E(G):
xxx=‘E‘
pass

class F(G):
xxx=‘F‘
pass

class B(E):
xxx=‘B‘
pass

class C(F):
xxx=‘C‘
pass

class D(G):
xxx=‘D‘
pass

class A(B,C,D):
xxx=‘A‘
pass


print(A.mro())













python面向对象编程

标签:course   teacher   people   对象   pre   ase   顺序   解决   eve   

原文地址:https://www.cnblogs.com/zhujunjie/p/9839514.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!