"# # Classic Class vs New class Person (object): #这就是新式类 pass #super (teacher,self). __init__ (name,age,sex) # New class-style person: # This is the classic class pass #School. __init__ (Self,name,age,sex) #经典类写法 # # New Class (first looking from left to right, B did not find C,c did not find a! This query called breadth query, first check the B,C layer, then go to a layer) class A (object): Def __init__ (self): self.n= "A" class B (a): Pass #def __init__ (self): #self. n= "B" Class C (A): def __init__ (self): self.n= "C" Class D (B,C): Pass # def __init__ (self): # self.n= "D" D=d () print (D.N) # # Classic Class (classic Class in Python2 first find b,b did not go to find a! This is called deep query, if not found in a to find C) (new class and classic class in the pythin3 are breadth query!) ) class A:def __init__ (self): self.n= ' A ' class B (a): #pass #def __init__ (self): #self. n= "B" Class C (a): #pass def __init__ (s ELF): self.n= "C" Class D (b,c): Pass #def __init__ (self): #self. n= "D" D=d () print (D.N) # polymorphic class Animal (object): Def __init__ (self, name): # Constructor of the class Self.name = ' name Def Talk ': # Abstract method, defined by convention only RA Ise Notimplementederror ("subclass must implement abstract method") class Cat (Animal): Def talk (self): #重新定义talk print ('%s : Meow meow! '% SELf.name) class Dog (Animal): Def talk (self): #重新定义talk print ('%s: Wang! Wang! Wang! '% self.name) def func (obj): # One interface, multiple forms (incoming different instances, called interfaces are talk, single expression different) Obj.talk () C1 = Cat (' small sunny ') D1 = Dog (' li Lei ') func (C1) func (D1)
New class vs. Classic Class, Class-polymorphic implementation