class people:
#定义基本属性
name = ‘’
age = 0
#定义私有属性,私有属性在类外部无法直接进行访问
__weight = 0
#定义构造方法
def init(self,n,a,w):
self.name = n
self.age = a
self.__weight = w
def speak(self):
print("%s 说: 我 %d 岁。身高%d" %(self.name, self.age, self.__weight))
class student():
grade = “”
def init(self, n, a ,w, g):
people.init(self, n, a, w)
self.grade = g
# 覆写父类的方法
def speak(self):
print("%s 说:我%d岁了, 我在读 %d 年纪" %(self.name, self.age, self.grade))
s = student(“小米”, 5, 5, 5)
s.speak()
class people:
def init(self, n, a):
self.name = n
self.age = a
def speak(self):
print("%s说:我%d 岁。" %(self.name, self.age))
s = people(“runoob”, 10)
s.speak()
构造函数
class people:
def init(self, n, a, w):
self.name = n
self.age = a
self._weight = w
def speak(self):
print("%s 说:我是%d岁" %(self.name, self.age))
d = people(“王耀”, 18, 5)
d.speak()
单继
class people:
def init(self, a, b):
self.name = a
self.age = b
# self.weigt =e
def speak(self):
print("%s说:我是%d岁" %(self.name, self.age))
class pop(people):
def init(self, a, b, j):
self.ooo = j
people.init(self, a, b)
def speak(self):
print("%s 说:我今年%d岁,我读%d年纪" %(self.name, self.age, self.ooo))
d = pop(“大爷”, 18, 5)
d.speak()
多继承
class people:
def init(self, a, b):
self.name = a
self.age = b
def speak(self):
print(“我叫%s,今年%d” %(self.name, self.age))
class student(people):
def init(self, a, b, c):
self.lihui =c
people.init(self, a, b)
def speak(self):
print(“我叫%s,今年%d岁,妈妈是个%s” %(self.name, self.age, self.lihui))
# q = student(“王耀”, 18, “老师”)
# q.speak()
class job:
def init(self, d, e):
self.mon= d
self.tue= e
def speak(self):
print(“大家好!我叫%s,今年%s岁” %(self.mon, self.tue))
# f1 =job(“小黄”, 2)
# f1.speak()
class sample(student,job):
def init(self, a, b, c, d, e, f):
self.wed = f
student.init(self, a, b, c)
job.init(self, d, e)
def speak(self):
print(“我叫%s,今年%d岁,妈妈是个%s,今天天气有点%s” %(self.name, self.age, self.lihui, self.wed))
f2 =sample(“小黄”,18,“老师”,“1”, 2, “冷”)
f2.speak()
方法重写
class people:
def lihui(self):
print(“父类的方法”)
class Child(people):
def lihui(self):
print(“子类的方法”)
d = Child()
d.lihui()
super(Child, d).lihui() # super() 函数是用于调用父类(超类)的一个方法。