• 面向对象编程-继承实例


     1 #!/usr/bin/env python
     2 # -*- coding:utf-8 -*-
     3 # Author:James Tao
     4 
     5 class School(object):
     6     def __init__(self,name,addr):
     7         self.name=name
     8         self.addr=addr
     9         self.students=[]
    10         self.teachers=[]
    11 
    12     def enroll(self,stu_obj):
    13         print('为学员%s办理注册手续' % stu_obj.name)
    14         self.students.append(stu_obj)
    15 
    16     def employ(self,tea_obj):
    17         print('为教师%s办理注册手续' % tea_obj.name)
    18         self.teachers.append(tea_obj)
    19 
    20 class SchoolMember(object):
    21     def __init__(self,name,age,sex):
    22         self.name=name
    23         self.age=age
    24         self.sex=sex
    25     def tell(self):
    26         print('''----
    27         ''')
    28 
    29 class Teacher(SchoolMember):
    30     def __init__(self, name, age, sex, salary, course):
    31         super(Teacher, self).__init__(name, age, sex)
    32         self.salary = salary
    33         self.course=course
    34 
    35     def tell(self):
    36         print('''
    37         ----info of {_name}----
    38         Name:{_name}
    39         Age:{_age}
    40         Sex:{_sex}
    41         Salary:{_salary}
    42         Course:{_course}
    43         '''.format(_name=self.name, _age=self.age, _sex=self.sex, _salary=self.salary, _course=self.course))
    44 
    45     def teach(self):
    46         print('%s is teaching course [%s]' % (self.name, self.course))
    47 
    48 class Student(SchoolMember):
    49     def __init__(self,name,age,sex,stu_id,grade):
    50         super(Student,self).__init__(name, age, sex)
    51         self.stu_id = stu_id
    52         self.grade=grade
    53 
    54     def tell(self):
    55         print('''
    56         ----info of {_name}----
    57         Name:{_name}
    58         Age:{_age}
    59         Sex:{_sex}
    60         Stu_id:{_stu_id}
    61         Grade:{_grade}
    62         '''.format(_name=self.name, _age=self.age, _sex=self.sex, _stu_id=self.stu_id, _grade=self.grade))
    63 
    64     def learn(self):
    65         print('%s is learning in grade [%s]' % (self.name, self.grade))
    66 
    67     def pay_tuition(self,amount):
    68         print('%s has paid tuition for $%s' % (self.name,amount))
    69 
    70 school=School('BIT','中关村')
    71 t1=Teacher('Wang',40,'F',200000,'BD')
    72 s1=Student('Tao',24,'M',3120180812,'BD')
    73 school.enroll(s1)
    74 school.employ(t1)
    75 t1.tell()
    76 s1.tell()
    77 print(school.students)
    78 school.students[0].learn()
    79 school.teachers[0].teach()

    运行结果:

  • 相关阅读:
    mysql查的用法
    js和jquery的基本应用
    JS实现html页面点击下载文件
    十二、PHP变量以及变量间传值的用法
    十一、jQuery的基本用法
    十、MySQL的应用大全
    九、使用盒子布局 margin 与padding如何使用
    在 Apex 中使用合并统计查询
    Salesforce 的 package.xml 文件
    Salesforce 应用生命周期管理
  • 原文地址:https://www.cnblogs.com/BIT-taozhen/p/10409952.html
Copyright © 2020-2023  润新知