python6.3 class inheritance and polymorphism

Animal class (Object):

DEF __init __ (Self, Color):
self.color = Color

DEF EAT (Self):
Print ( "! animals eat")

DEF RUN (Self):
Print ( "! animals run")

class cat (Animal): # inherits the Animal class
DEF EAT (Self):
Print ( "cat fish!")

class Dog (Animal):
DEF __init __ (Self, name, Age, Color):
Super (Dog, Self). __init __ (color) # call the initialization method of the parent class
self.name = name
self.age = Age
DEF EAT (Self):
Print ( "dog gnawing on a bone!")
inherited class #
cat = Cat ( "black")
Print (cat.color)
cat.eat ()
cat.run ()
Dog Dog = ( "white", 7 "black")
dog.eat ()
dog.run ()


# Polymorphic class
DEF Feed (obj):
obj.eat ()
AN = Animal ( "yellow")
CAT Cat = ( "orange")
Dog Dog = ( "black", 5, "black")
Feed ( cat)

 

Guess you like

Origin www.cnblogs.com/lma0702/p/11111041.html