New class VS Classic class, multi-state implementation of class

Source: Internet
Author: User

"# # 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.