类的多态

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

原文地址:https://www.cnblogs.com/ly570/p/11408530.html