Object-oriented 2,

Source: Internet
Author: User

Object-oriented 2,
Class inheritance

1 class person (object): 2 def _ init _ (self, name, age): 3 self. name = name 4 self. age = age 5 6 def info (self): 7 print ("the person's name is % s age is % d" % (self. name, self. age) 8 9 class yello_person (person): # inherit from the person class 10 def _ init _ (self, name, age, level): 11 person. _ init _ (self, name, age) # The constructor first inherits the parent class and adds the attribute parameter 12 self. level = level13 14 def show_level (self): 15 print ("the person % s level is % d" % (self. name, self. level) 16 17 y1 = yello_person ('zsq ', 28, 15) 18 y1.info () # Use the method 19 y1.show _ level () that inherits the parent class # Use the method of the subclass

In the code above, yello_person inherits the person parent class.

The constructor In the subclass first inherits the constructor of the parent class, and then adds its own special attributes.

Lines 18 and 19 show how to use an object to call the methods of the parent class and subclass respectively.

1 class SchoolMember (object): 2 Member = 0 3 def _ init _ (self, name, age): 4 self. name = name 5 self. age = age 6 SchoolMember. regist (self) # The constructor calls the registration method directly, and Adds 1 7 def regist (self): 8 SchoolMember to the number of members. member + = 1 # public attribute 9 10 def display (self): # print the method 11 print ('% s info is --' % self. name) 12 for k, v in self. _ dict __. items (): 13 print ('\ t', k,' \ t', v) 14 print ('end --- ') 15 16 def _ del _ (self): # destructor at hand When an object is deleted, it is executed. Or the entire program will be executed on all objects at the end of the program, which will be printed at the end of the program. 17 print ('remove % s' % self. name) 18 19 class Teacher (SchoolMember): 20''' 21 defines the Teacher subclass and automatically registers during initialization. Add attributes and Methods 22 ''' 23 def _ init _ (self, name, age, salay, course): 24 SchoolMember. _ init _ (self, name, age) 25 self. salary = salay26 self. couesr = course27 def teaching (self): 28 print ("% s teaching % s" % (self. name, self. couesr) 29 30 class Student (SchoolMember): 31 def _ init _ (self, name, age, tuition): 32 SchoolMember. _ init _ (self, name, age) 33 self. tuition = tuition34 def tuition (self): 35 print ('% s pay money % d for study' % (self. name, self. tuition) 36 37 38 t1 = Teacher ('zsq ', 28,150 00, "cloud_compute") 39 t1.teaching () # Call The subclass Method 40 t1.display () # Call the parent class Method 41 print (SchoolMember. member) # print the school Member and Add 1 automatically when the object is initialized.

 

The six lines in the code above demonstrate how to operate public attributes in the constructor to implement a global count function.

Example of a method defined in 10 rows: how to print all attributes of an object in the parent class

 

Polymorphism

Unified calling interfaces provide different processing logic for different objects.

1 class Animal (object): 2 def talk (self): 3 print ('this is father class') 4 5 class Dog (Animal): 6 def talk (self ): 7 print ("trademanager") 8 9 class Cat (Animal): 10 def talk (self): 11 print ("too many meow") 12 13 d1 = Dog () 14 c1 = Cat () 15 def animal_talk (obj): # currently, calling different objects through unified interfaces produces different results, that is, polymorphism. You can use the example function as the transition scheme 16 obj. talk () # Here obj is a specific object and calls the talk method of the object. That is, the method after reconstruction in the subclass to realize polymorphism. 17 animal_talk (d1) 18 animal_talk (c1)

 

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.