Python zero-basic entry 14 inheritance

Source: Internet
Author: User
In my previous blog, I briefly talked about the syntax of class inheritance. today I will talk about the class-related knowledge. In my previous blog, I briefly talked about the syntax of class inheritance. today I will talk about the class-related knowledge.
First, use a piece of code to review the inheritance of the classes you have learned before:

Class Parent: def hello (self): print ("The method of the Parent class is being called ..... ") class Child (Parent): # pass def hello (self): print (" The subclass method is being called ..... ") p = Parent () p. hello () c = Child () c. hello ()

(1) If the subclass defines a method or attribute with the same name as the parent class, it will automatically overwrite the method or attribute corresponding to the parent class.

Import random as rclass Fish: def _ init _ (self): self. x = r. randint (0, 10) self. y = r. randint (0, 10) def move (self): self. x-= 1 print ("My location is:", self. x, self. y) class Goldfish (Fish): passclass Carp (Fish): passclass Salmon (Fish): passclass Shark (Fish): def _ init _ (self ): # Call the unbound parent class method and pass in the instantiated object of the subclass # Fish. _ init _ (self) # Use The super () function super (). _ init _ () self. hungry = True def eat (self): if self. hungry: print ("The dream of food is to eat every day") self. hungry = False else: print ("too strong, not enough") shark = Shark () shark. move () # In this case, the program will report an error because the Shark subclass overwrites the #__ init _ method, which overwrites the _ init _ method of the parent class.

(2) Python also supports multiple inheritance

Class Base1: def foo1 (self): print ("I'm foo1, and I speak for Base1... ") class Base2: def foo2 (self): print (" I'm foo2, and I speak for Base2... ") class C (Base1, Base2): passc = C () c. foo1 () c. foo2 ()

For example, the Turtle, Fish, and Pool classes in the following code do not have obvious inheritance relationships. However, using combinations can easily solve the problems between these classes. The following code solves the problem of the number of goldfish and turtles in the output pond. The main solution is to instantiate turtles and golden fish into the pond class.

Class Turtle: def _ init _ (self, x): self. num = xclass Fish: def _ init _ (self, x): self. num = xclass Pool: def _ init _ (self, x, y): # put the class instantiation in another class self. turtle = Turtle (x) self. fish = Fish (y) def print_num (self): print ("total turtles % d in the pool, % d fish! "% (Self. turtle. num, self. fish. num) pool = Pool (1, 10) pool. print_num ()

Finally, I will add some built-in functions for the class:

# The first is A subclass, followed by A base class # issubclass (class, classinfo) # judge whether A class is A subclass of another class # A class is considered its own subclass class: passclass B (A): passprint (issubclass (B, A) # isinstance (object, classinfo) # The first is an instantiated object, next is the class # check whether the instance object belongs to this class b1 = B (); print (isinstance (b1, B) # hasattr (object, name) # test whether an object has a specific attribute class C: def _ init _ (self, x = 0): self. x = x # note that Python does not have a variable definition c1 = C () print (hasattr (c1, "x") # getattr (object, name [, default]) # return the attribute value specified by the object. if it does not exist, print the default value print (getattr (c1, 'x') print (getattr (c1, 'y ', "The parameter you accessed does not exist") # setattr (object, name, value) # sets the attribute value. if it does not exist, setattr (c1, "y ", "Yaoxiangxi") print (getattr (c1, 'y', "your access parameter does not exist") # delattr (object, name) # Delete the attribute delattr (c1, 'Y') # property (fget = None, fset = None, fdel = None, doc = None) # set the attribute class C: def _ init _ (self, size = 10): self. size = size def getSize (self): return self. size def setSize (self, value): self. size = value def delSize (self): del self. size # Delete an attribute # facilitate code modification x = property (getSize, setSize, delSize) c1 = C () print (c1.getSize () print (c1.x) c1.x = 100 print (c1.x)

The final property will be explained in detail in the descriptor blog.

The above is the inheritance of Python. For more information, see PHP Chinese website (www.php1.cn )!

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.