vim inherit.py
#!/usr/bin/env python
class SchoolMember: #申明一个基类
def__init__(self,name,gender,nationality='CN'): #初始化基类
self.name =name #定义类的属性
self.gender =gender
self.nation =nationality
def tell(self): #定义基类的一个方法
print 'Hi,myname is %s,I am from %s' %(self.name,self.nation)
class Student(SchoolMember): #子类继承基类
def__init__(self,Name,Gender,Class,Score,Nation):#类Student的初始化
SchoolMember.__init__(self,Name,Gender,Nation) #继承基类,将student的参数传给基类,将局部变量变成基类的全局变量
self.Class =Class
self.Score =Score
defpayTuition(self,amount):
if amount <6499:
print 'Getthe fuck off...'
else:
print'Welcome onboard!'
class Teacher(SchoolMember):
def__init__(self,Name,Gender,Course,Salary,Nation):
SchoolMember.__init__(self,Name,Gender,Nation)
self.Course =Course
self.Salary =Salary
defteachering(self):
print 'i amteaching %s, i am making %s per month!' %(self.Course,self.Salary)
S1 = Student('WangFanHao','Male','Python','C+','JP')
S1.tell()
S1.payTuition(5000)
S2 = Student('ShitTshirt','Male','Python','B','JP')
S2.tell()
S2.payTuition(6500)
T1=Teacher('Alex','Male','C++',5000,'CN')
T1.tell()
T1.teachering()
结果:
[root@node110 class]# python inherit.py
Hi,my name is WangFanHao,I am from JP
Get the fuck off...
Hi,my name is ShitTshirt,I am from JP
Welcome onboard!
Hi,my name is Alex,I am from CN
i am teaching C++, i am making 5000 per month!