码迷,mamicode.com
首页 > 编程语言 > 详细

Python 面向对象4-继承

时间:2018-09-22 19:45:38      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:sel   The   on()   teacher   学费   print   重写   object   作者   

#!/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()

Python 面向对象4-继承

标签:sel   The   on()   teacher   学费   print   重写   object   作者   

原文地址:https://www.cnblogs.com/Presley-lpc/p/9690790.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!