大家好,又见面了,我是你们的朋友全栈君。
#-*-coding:utf-8-*-
'''
使用继承
'''
class SchoolMember:
def __init__(self,name,age,addr,hoppy):
self.name = name
self.age = age
self.addr = addr
self.hoppy = hoppy
print('初始化的名字是%s'%self.name)
def tell(self):
print('姓名:%s,年龄:%s,地址:%s,爱好:%s'%(self.name,self.age,self.addr,self.hoppy))
class Teacher(SchoolMember):
def __init__(self,name,age,addr,hoppy,salary):
SchoolMember.__init__(self,name,age,addr,hoppy)
self.salary = salary
print('继承SchoolMember传入的名字:%s'%self.name)
def tell(self):
SchoolMember.tell(self)
print('我的工资一般是:%s'%self.salary)
class Student(SchoolMember):
def __init__(self,name,age,addr,hoppy,marks):
SchoolMember.__init__(self,name,age,addr,hoppy)
self.marks=marks
print('我是继承SchoolMember学生%s'%self.name)
def tell(self):
SchoolMember.tell(self)
print('我这次的成绩是:%d'%self.marks)
t = Teacher('dcy','40','河南郑州','旅游',3000)
s = Student('mxl',22,'上海','导游',85)
members = [t,s]
for member in members:
member.tell()
class Father:
def __init__(self):
print('我是初始化Father类中的方法')
print('供以后调用')
class Son(Father):
def __init__(self):
print('我是初始化Son类中的方法')
Father.__init__(self)
b = Son()
'''
为了解决类名变动后引起大批量修改,需要使用关键字Super来调用父类中的方法
'''
class PersonStar(object):
def __init__(self,name):
self.name = name
print('我是一个人,初始化的名字是:%s'%self.name)
class Star(PersonStar):
def __init__(self,name):
super(Star,self).__init__(name)
print('我是一个大明星')
if __name__ == '__main__':
Star('dcy')
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/156025.html原文链接:https://javaforall.cn