类的多态

时间:2019-08-25 18:12:09   收藏:0   阅读:57

多态的要素
继承
重写方法
实例
class Program(object):
name = ‘Mike‘

def __init__(self, age, sex, weight):
self.age = age
self._sex = sex
self.__weight = weight

def intro(self):
print(‘my name is ‘, self.name)
print(‘my age is ‘, self.age)


class BProgram(Program):
def __init__(self, age, sex, weight, language):
super(BProgram, self).__init__(age, sex, weight)
self.language = language

def intro(self):
print(‘my age is %s\nmy language is %s‘ % (self.age, self.language))


def introduction(program):
if isinstance(program, Program):
program.intro()


if __name__ == ‘__main__‘:
program = Program(12, ‘female‘, 34)
bProgram = BProgram(22, ‘male‘, 50, ‘python‘)
introduction(program)
introduction(bProgram)
————————————————

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!