How to Get Started python rewrite become the god

A method for override python class learn very important, in addition to basic python, that the essence of the python inheritance, polymorphism, encapsulation and decorators; The following is a simple example of inheritance rewritten:

# 父类
class Student():
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def introduce(self):
        print('我的名字叫{},我今年{}岁了'.format(self.name, self.age))

# 子类
class NewStudent(Student):
    def __init__(self, name, age, sex):
        Student.__init__(self, name, age)
        self.sex = sex
	
	# 重写父类introduce方法,是父类中的introduce失效
    def introduce(self):
        print('我的名字叫{},我今年{}岁了, 我是{}孩。'.format(self.name, self.age,self.sex))


s = NewStudent('小明', 18, '男')
s.introduce()
Published 47 original articles · won praise 18 · views 20000 +

Guess you like

Origin blog.csdn.net/human_soul/article/details/104733648