【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")
欢迎访问我的个人博客站点:
https://yeyeck.com