zoukankan      html  css  js  c++  java
  • 【python】面向对象

    1.简单写个例子,因为之前基本没写过python OOP。先写个父类

    class Animal:
    
        # 构造方法
        def __init__(self, name):
            self.name = name
    
        # 公有方法
        def run(self):
            print(self.name, "is running")
    
        def sing(self, song):
            print(self.name, "is singing:", song)
    
        def eat(self, food):
            print(self.name, "is eating", food)

    测试代码

    if __name__ == '__main__':
       
        panda = Animal("Panda")
        dog = Animal("dog")
        cat = Animal("cat")
    
        panda.run()
        panda.eat("pork")
        panda.sing("I love bamboo")
    
        dog.run()
        dog.eat("bone")
        dog.sing("I am a happy dog")
    
        cat.run()
        cat.eat("bamboo")
        cat.sing("I believe I am a tiger")

    输出

    Panda is running
    Panda is eating pork
    Panda is singing: I love bamboo
    dog is running
    dog is eating bone
    dog is singing: I am a happy dog
    cat is running
    cat is eating bamboo
    cat is singing: I believe I am a tiger

    这个测试结果我们发现,不管给什么,它都吃。于是我们希望不同的动物对同一个动作 (方法)有不同的表现形式(实现)

    写子类

    class Panda(Animal):
        foods = ("bamboo",)
    
        def __init__(self):
            super(Panda, self).__init__("Panda")  # 调用父类构造方法
    
        def eat(self, food):      # 重写父类 eat方法
            if food in self.foods:
                super().eat(food)
            else:
                print(self.name, "doesn't like", food)
    
    
    class Dog(Animal):
        foods = ("pork", "bone", "mouse")
    
        def __init__(self):
            super(Dog, self).__init__("Dog")
    
        def eat(self, food):    # 重写父类eat方法
            if food in self.foods:
                super().eat(food)
            else:
                print(self.name, "doesn't like", food)
    
    
    class Cat(Animal):
        foods = ("mouse", "pork")
    
        def __init__(self):
            super(Cat, self).__init__("Cat")
    
        def eat(self, food):   # 重写父类eat方法
            if food in self.foods:
                super().eat(food)
            else:
                print(self.name, "doesn't like", food)
    
        def sing(self, song):
            print(self.name, "is singing", "mia~ mia~ mia~")

    测试代码

    if __name__ == '__main__':
        panda = Panda()
        dog = Dog()
        cat = Cat()
    
        panda.run()
        panda.eat("pork")
        panda.sing("I love bamboo")
    
        dog.run()
        dog.eat("bone")
        dog.sing("I am a happy dog")
    
        cat.run()
        cat.eat("bamboo")
        cat.sing("I believe I am a tiger")

    输出

    Panda is running
    Panda doesn't like pork
    Panda is singing: I love bamboo
    Dog is running
    Dog is eating bone
    Dog is singing: I am a happy dog
    Cat is running
    Cat doesn't like bamboo
    Cat is singing mia~ mia~ mia~

    最终代码

    class Animal:
    
        # 构造方法
        def __init__(self, name):
            self.name = name
    
        # 公有方法
        def run(self):
            print(self.name, "is running")
    
        def sing(self, song):
            print(self.name, "is singing:", song)
    
        def eat(self, food):
            print(self.name, "is eating", food)
    
    
    class Panda(Animal):
        foods = ("bamboo",)
    
        def __init__(self):
            super(Panda, self).__init__("Panda")  # 调用父类构造方法
    
        def eat(self, food):      # 重写父类 eat方法
            if food in self.foods:
                super().eat(food)
            else:
                print(self.name, "doesn't like", food)
    
    
    class Dog(Animal):
        foods = ("pork", "bone", "mouse")
    
        def __init__(self):
            super(Dog, self).__init__("Dog")
    
        def eat(self, food):    # 重写父类eat方法
            if food in self.foods:
                super().eat(food)
            else:
                print(self.name, "doesn't like", food)
    
    
    class Cat(Animal):
        foods = ("mouse", "pork")
    
        def __init__(self):
            super(Cat, self).__init__("Cat")
    
        def eat(self, food):   # 重写父类eat方法
            if food in self.foods:
                super().eat(food)
            else:
                print(self.name, "doesn't like", food)
    
        def sing(self, song):
            print(self.name, "is singing", "mia~ mia~ mia~")
    
    
    if __name__ == '__main__':
        panda = Panda()
        dog = Dog()
        cat = Cat()
    
        # panda = Animal("Panda")
        # dog = Animal("dog")
        # cat = Animal("cat")
    
        panda.run()
        panda.eat("pork")
        panda.sing("I love bamboo")
    
        dog.run()
        dog.eat("bone")
        dog.sing("I am a happy dog")
    
        cat.run()
        cat.eat("bamboo")
        cat.sing("I believe I am a tiger")
  • 相关阅读:
    P4936 题解
    初赛
    洛谷P2763题解
    探秘最小生成树&&洛谷P2126题解
    洛谷P2630 题解
    洛谷P2125 题解
    洛谷P1510 题解
    洛谷P3572题解
    Codeforces 448C Painting Fence(分治法)
    Codeforces 999F Cards and Joy(二维DP)
  • 原文地址:https://www.cnblogs.com/yeyeck/p/9533824.html
Copyright © 2011-2022 走看看