"python38--Object-oriented inheritance"

Source: Internet
Author: User

First, inheritance

1. Syntax: Class Derivedclassname (Baseclassname): Subclass Inherits Parent class

 >>> class   Parent:  def   Hello (self):   Print  ( "  Calling the method of the parent class ...   " )  >>> class   Child (Parent):  pass  >>> P = Parent ()  >>> P.hello () is calling the method of the parent class ...  >>> C = child ()  >>> C.hello () is calling the method of the parent class ...  >>> #   Subclass Child inherits the parent class, So subclasses can call methods of the parent class C.hello ()  

2. Methods and properties corresponding to the parent class are automatically overwritten if the methods and properties defined in the subclass are the same as the properties and method names of the parent class

Import Random as R

Class 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 ', SELF.X,SELF.Y)

Class Goldfish (Fish):
Pass

Class Carp (Fish):
Pass

Class Salmon (Fish):
Pass

Class Shark (Fish):
def __init__ (self):
Self.hungry = True

Def eat (self):
If Self.hungry:
Print (' Foodie's dream is to eat meat every day! ‘)
Self.hungry = False
Else
Print (' Eat up! ‘)

>>> fish = fish ()
>>> Fish.move ()
My position 4 7
>>> goldfish = Goldfish ()
>>> Goldfish.move ()
My position 6 5
>>> carp = Carp ()
>>> Carp.move ()
My position 7 6
>>> salmon = Salmon ()
>>> Salmon.move ()
My position-1 10
>>> shark = Shark ()
>>> Shark.move ()
Traceback (most recent):
File "<pyshell#9>", line 1, in <module>
Shark.move ()
File "/users/yixia/desktop/fish.py", line 9, in move
Self.x-=1
Attributeerror: ' Shark ' object has no attribute ' x '

---cause of the error: Attributeerror: ' Shark ' object has no attribute ' x ': Shark is not an attribute of X, Shark inherits Fish, why is there no attribute of x?

Cause: Shark overrides the __init__ method, overwriting the X attribute in the __init__ () method of the parent fish, that is, the subclass defines the class as a method with the same name as the parent class, overwriting the methods and properties of the parent class

Corrected code:

Two methods: 1, call the unbound parent class Method 2, use the super () function

The implementation code is as follows:

The first type:

Import Random as R

Class 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 ', SELF.X,SELF.Y)

Class Goldfish (Fish):
Pass

Class Carp (Fish):
Pass

Class Salmon (Fish):
Pass

Class Shark (Fish):
def __init__ (self):
fish.__init__ (self) #父类名称调用__init__ () function
Self.hungry = True

Def eat (self):
If Self.hungry:
Print (' Foodie's dream is to eat meat every day! ‘)
Self.hungry = False
Else
Print (' Eat up! ‘)

>>> shark = Shark ()
>>> Shark.move ()
My position 1 8
>>>

The second type:

Import Random as R

Class 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 ', SELF.X,SELF.Y)

Class Goldfish (Fish):
Pass

Class Carp (Fish):
Pass

Class Salmon (Fish):
Pass

Class Shark (Fish):
def __init__ (self):
super (). __init__ () #采用super () function
Self.hungry = True

Def eat (self):
If Self.hungry:
Print (' Foodie's dream is to eat meat every day! ‘)
Self.hungry = False
Else
Print (' Eat up! ‘)

>>> shark = Shark ()
>>> Shark.move ()
My position-1 6

"python38--Object-oriented inheritance"

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.