美文网首页编程
Python基础学习8

Python基础学习8

作者: ericblue | 来源:发表于2018-12-21 10:30 被阅读0次

面向对象编程:
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

    本文链接:https://www.haomeiwen.com/subject/wgojhqtx.html