pythonl类继承例子

Posted 夏晓旭

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了pythonl类继承例子相关的知识,希望对你有一定的参考价值。

#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类继承例子的主要内容,如果未能解决你的问题,请参考以下文章

类继承 - 是否可以将所有继承的方法转换为子类类型?

JAVA多继承例子

JAVA多继承例子

多继承 与 多重继承

python2中的新式类与经典类区别

python2中的新式类与经典类区别