1. 程式人生 > >pythonl類繼承例子

pythonl類繼承例子

rip AC ret main nbsp utf-8 __name__ clas pri

#coding=utf-8

class Person(object):
def __init__(self,name,age):
self.name=name
self.age=age

def get_age(self):
return self.age

def get_name(self):
return self.name

def set_age(self,age):
if age>=0 and age<=150:
self.age=age

def set_name(self,name):
self.name=name

class SchoolMember(Person):
def __init__(self,name,age,school_name):
Person.__init__(self,name,age)
self.school_name=school_name

def get_school_name(self):
return self.school_name

def set_school_name(self,school_name):
self.school_name=school_name

class Student(SchoolMember):
def __init__(self,name,age,school_name,grade):
SchoolMember.__init__(self,name,age,school_name)
self.grade=grade

def get_grade(self):
return self.grade

def set_grade(self,grade):
self.grade=grade

class Teacher(SchoolMember):
def __init__(self,name,age,school_name,level):
SchoolMember.__init__(self,name,age,school_name)
self.level=level

def get_level(self):
return self.level

def set_level(self,level):
self.level=level


if __name__==‘__main__‘:
p=Person(‘xiaxiaoxu‘,18)
print p.get_age()
print p.get_name()
p.set_name(‘sam xia‘)
p.set_age(20)
print p.get_age()
print p.get_name()
s=SchoolMember(‘xiaxiaoxu‘,18,‘dalian maritime university‘)
print s.get_school_name()
s.set_school_name(‘peking university‘)
print s.get_school_name()
t=Student(‘xiaxiaoxu‘,20,‘peking university‘,‘grade one‘)
print t.get_grade()
t.set_grade(‘grade two‘)
print t.get_grade()
th=Teacher(‘xiaxiaoxu‘,20,‘dalian maritime university‘,‘professor‘)
print th.get_level()
th.set_level(‘teacher‘)
print th.get_level()

c:\Python27\Scripts>python task_test.py
18
xiaxiaoxu
20
sam xia
dalian maritime university
peking university
grade one
grade two
professor
teacher

pythonl類繼承例子