美文网首页
Python继承

Python继承

作者: 古寒飞 | 来源:发表于2017-04-18 19:59 被阅读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())

    相关文章

      网友评论

          本文标题:Python继承

          本文链接:https://www.haomeiwen.com/subject/ckyqzttx.html