学习记录 日期:2021年9月4日
python中类成员的继承和重写
class Person:
def __init__(self,name,age):
self.name = name
self.__age = age
def say_age(self):
print('{0}的年龄是{1}'.format(self.name,self.__age))
def say_introduce(self):
print('My name is{0}'.format(self.name))
class Student(Person):
def __init__(self,name,age,score):
Person.__init__(self,name,age)
self.score = score
def say_introduce(self):
'''重写父类的方法'''
print('报告老师,我的名字是:{0}'.format(self.name))
s = Student('James',18,90)
s.say_age()
s.say_introduce()
python中的object类
class Person:
def __init__(self,name,age):
self.name = name
self.age = age
def say_age(self):
print(self.name,'的年龄是:',self.age)
obj = object()
print(dir(obj))
s2 = Person('James',19)
print(dir(s2))
python重写__str__方法
class Person:
def __init__(self,name):
self.name = name
def __str__(self):
return '名字是:{0}'.format(self.name)
p = Person('James')
print(p)
python中类的多重继承
class A:
def aa(self):
print('aa')
class B:
def bb(self):
print('bb')
class C(B,A):
def cc(self):
print('cc')
c = C()
c.cc()
c.bb()
c.aa()
python中的mro()方法
class A:
def aa(self):
print('aa')
def say(self):
print('你好啊,A兄弟')
class B:
def bb(self):
print('bb')
def say(self):
print('你啊好啊,B兄弟')
class C(A,B):
def cc(self):
print('cc')
c = C()
print(C.mro())
c.say()
python中_super获得父类的定义
class A:
def say(self):
print('A',self)
class B(A):
def say(self):
super().say()
print('B',self)
B().say()
python中的多态
class Man:
def eat(self):
print('饿了,吃饭啦')
class Chinese(Man):
def eat(self):
print('中国人用筷子吃饭')
class English(Man):
def eat(self):
print('英国人用叉子吃饭')
class Indian(Man):
def eat(self):
print('印度人用右手吃饭')
def manEat(m):
if isinstance(m,Man):
m.eat()
else:
print('不能吃饭')
manEat(Chinese())
manEat(English())
|