类的多态

多态的要素
继承
重写方法
实例
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)
————————————————

posted on 2019-08-25 17:14  激流勇进1  阅读(114)  评论(0编辑  收藏  举报