Python继承

2017-04-18  本文已影响22人  古寒飞

.#!/usr/bin/python

class Person(object):
def init(self,name):
self.name=name
def get_details(self):
return self.name

class Student(Person):
def init(self,name,branch,year):
Person.init(self,name)
self.branch=branch
self.year=year
def get_details(self):
return "{} student {} and is in {} year".format(self.name,self.branch,self.year)

class Teacher(Person):
def init(self,name,papers):
Person.init(self,name)
self.papers=papers
def get_details(self):
return "{} teacher {}".format(self.name,','.join(self.papers))

person1=Person('aaa')
student1=Student('bbb','English',2005)
teacher1=Teacher('ccc',['C','C++'])

print(person1.get_details())
print(student1.get_details())
print(teacher1.get_details())

上一篇下一篇

猜你喜欢

热点阅读