Python基础学习8

面向对象编程:
1.类名称首字母大写其余小写。
2.类的封装使用是在变量名前加__,例如:self.__name = name
3.继承父类时,子类使用父类变量属性,可以使用super()方法可借用父类初始化,不需要重新耗用资源和代码初始化。
继承父类举例:下面可用self可用super替代

class Monster():
    '定义怪物类'
    def __init__(self,hp=100):
        self.hp = hp
    def run(self):
        print('移动到某个位置')

class Animals(Monster):
    '普通怪物'
    def __init__(self,hp=10):
        self.hp = hp#使用下面super替代
        super().__init__(hp)

你可能感兴趣的:(Python基础学习8)