zoukankan      html  css  js  c++  java
  • python之面向对象之继承

    #写一个类SchoolMember
    class SchoolMember(object):
        member_num = 0
        def __init__(self,name,age,sex):
            self.name = name
            self.age = age
            self.sex = sex
            self.reg()
            # self.member_num += 1
        def reg(self):
            SchoolMember.member_num += 1
            print("SchoolMember[%s] is come to school,SchoolMember [%s] is already registered" %(self.member_num,self.name))
        def tell(self):
            print("hello,my name is [%s],and my age is [%s]" %(self.name,self.age))
    
    
    #写一个SchollMember的子类teacher
    class Teacher(SchoolMember):
        def __init__(self,name,age,sex,course,salary):
            super(Teacher,self).__init__(name,age,sex)
            self.course = course
            self.salary = salary
        def teaching(self):
            print("Teacher [%s] is teaching [%s]" %(self.name,self.course))
    
    
    class Student(SchoolMember):
        def __init__(self,name,age,sex,course,xufei):
            super(Student,self).__init__(name,age,sex)
            self.course = course
            self.xufei = xufei
        def pay_money(self):
            print("student [%s] must pay [%s] to school" %(self.name,self.xufei))
    
    
    
    #首先分别创建2个teacher的实例和2个student的实例
    
    print("首先分别创建2个teacher的实例和2个student的实例")
    t1 = Teacher("teacher1","12","M","python","123")
    t2 = Teacher("teacher2","13","M","shell","234")
    s1 = Student("Student1","1","W","python","1234")
    s2 = Student("Student2","2","M","shell","12345")
    
    # 首先分别创建2个teacher的实例和2个student的实例
    # SchoolMember[1] is come to school,SchoolMember [teacher1] is already registered
    # SchoolMember[2] is come to school,SchoolMember [teacher2] is already registered
    # SchoolMember[3] is come to school,SchoolMember [Student1] is already registered
    # SchoolMember[4] is come to school,SchoolMember [Student2] is already registered
    
    
    print("teacher调用父类SchoolMember的方法tell")
    t1.tell()
    t2.tell()
    
    # teacher调用父类SchoolMember的方法tell
    # hello,my name is [teacher1],and my age is [12]
    # hello,my name is [teacher2],and my age is [13]
    
    
    print("teacher实例调用teacher类的方法")
    t1.teaching()
    t2.teaching()
    
    # teacher实例调用teacher类的方法
    # Teacher [teacher1] is teaching [python]
    # Teacher [teacher2] is teaching [shell]
    
    
    print("student调用父类SchoolMember的方法tell")
    s1.tell()
    s2.tell()
    
    # student调用父类SchoolMember的方法tell
    # hello,my name is [Student1],and my age is [1]
    # hello,my name is [Student2],and my age is [2]
    
    
    print("student实例调用student类的方法")
    s1.pay_money()
    s2.pay_money()
    
    
    # student实例调用student类的方法
    # student [Student1] must pay [1234] to school
    # student [Student2] must pay [12345] to school
    

      

  • 相关阅读:
    读书笔记之:C语言核心技术
    读书笔记之:C++Primer 第4版(ch111)
    读书笔记之:C与指针
    读书笔记之:C专家编程
    读书笔记之:C/C++代码精髓
    浮点数在内存中的表示
    读书笔记之:C++Primer 第4版(ch1214)
    C/C++语言中const的用法
    比NotePad++更好的文本代码(C#)编辑器Sublime Text
    ExtJs十四(ExtJs Mvc图片管理之四)
  • 原文地址:https://www.cnblogs.com/bainianminguo/p/7190862.html
Copyright © 2011-2022 走看看