码迷,mamicode.com
首页 > 其他好文 > 详细

类的多态

时间:2019-08-25 18:12:09      阅读:57      评论:0      收藏:0      [点我收藏+]

标签:__name__   ike   obj   super   self   ash   多态   das   %s   

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

类的多态

标签:__name__   ike   obj   super   self   ash   多态   das   %s   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!