zoukankan      html  css  js  c++  java
  • 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:Python27Scripts>python task_test.py
    18
    xiaxiaoxu
    20
    sam xia
    dalian maritime university
    peking university
    grade one
    grade two
    professor
    teacher

  • 相关阅读:
    Java 9 揭秘(14. HTTP/2 Client API)
    Java 9 揭秘(13. Collection API 更新)
    Java 9 揭秘(12. Process API 更新)
    JAVA数组与List相互转换
    linux下开启oracle服务和开启监听
    Centos7安装Redis
    Hibernate乐观锁无法Catch到org.hibernate.StaleObjectStateException
    Eclipse远程调试Tomcat
    Centos7 使用Docker搭建Oracle测试环境
    Maven安装Oracle驱动包到本地仓库
  • 原文地址:https://www.cnblogs.com/xiaxiaoxu/p/8778278.html
Copyright © 2011-2022 走看看