首页 > 编程语言 > 详细

pythonl类继承例子

时间:2018-04-10 14:11:04      阅读:217      评论:0      收藏:0      [点我收藏+]

#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类继承例子

原文:https://www.cnblogs.com/xiaxiaoxu/p/8778278.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!