Python学习笔记_类

class Animal(object): # 定义父类animal
    def __init__(self,name,sound): # 初始化属性 name sound
        self.name = name
        self.sound = sound
        self.talk() # 一实例化即调用talk()方法

    def talk(self): # 定义父类方法,用于打招呼
        print("hello,i am %s,%s" % (self.name,self.sound))

class Dog(Animal): # 继承父类animal
    def jump(self): # 增加子类新方法
        print("i love jumping!")

class Cat(Animal): # 继承父类animal
    def sleep(self): # 增加子类新方法
        print("i love sleeping")


catt = Cat("cat","miao~miao~miao") # 实例化cat类,得到catt对象
catt.sleep() # 调用对象方法
dogg = Dog("dog","wang~wang~wang")
dogg.jump()
posted @ 2020-11-22 23:50  止一  阅读(61)  评论(0编辑  收藏  举报