Python 面向对象4-继承

2021-06-19 08:06

阅读:332

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# 作者:Presley
# 邮箱:1209989516@qq.com
# 时间:2018-08-05
# OOP学习2

class SchoolMember(object):
member_numbers = 0
def __init__(self,name,age,sex):
self.name = name
self.age = age
self.sex = sex
self.enroll() #注册
def enroll(self):
SchoolMember.member_numbers += 1
print("The [{0}] school member [{1}] is enrolled!".format(self.member_numbers,self.name))

def tell(self):
print("Hello my name is %s"%(self.name))

class Teacher(SchoolMember):
def __init__(self,name,age,sex,course,salary): #course 课程 ,salary 薪水
super(Teacher,self).__init__(name,age,sex)#先定义重写父类的name,age,sex这三个值,然后把父类的name,age,sex的值拿过来
self.course = course
self.salary = salary
#SchoolMember.__init__(self,name,age,sex)与super方法一样
def teaching(self):
print("Teacher [{0}] is teaching [1]".format(self.name,self.course))

class Student(SchoolMember):
def __init__(self,name,age,sex,course,tuition): #tuition学费
super(Student,self).__init__(name,age,sex)
self.course = course
self.tuition = tuition
def pay_tution(self): #交学费
print("cao,student [{0}] paying tution [{1}]".format(self.name,self.tuition))

t1 = Teacher("Alex",22,‘F‘,"PY",1000)
t2 = Teacher("Tenglan",25,"N/A","PY",900)

s1 = Student("SanJiang",24,"Female","python",15000)
s2 = Student("BaoAn",23,"F","python",5000)

t1.tell()
t1.teaching()

s1.pay_tution()


评论


亲,登录后才可以留言!