Python Object-oriented (iii) inheritance

Source: Internet
Author: User

Inherited

Introduced

Inheritance is the derivation of new classes from existing classes, which can absorb data properties and behaviors of existing classes, and can extend new capabilities. Inheritance is often said to be the is-a relationship. Subclasses inherit the characteristics and behavior of the parent class, making the subclasses have various properties and methods of the parent class. Or subclass inherits a method from the parent class so that the child class has the same behavior as the parent class.

Example:
For example, you can define a class to call a car, the car has the following properties: Body size, color, steering wheel, tires, and the car this class derived from cars and trucks two classes, add a small trunk for the car, and add a large container for the truck.

Inheritance is expressed as an intersection between object classes, which enables a class of objects to inherit data members and member methods of another class of objects. If Class B inherits from Class A, then the object belonging to B has all or part of the nature of Class A (data attributes) and functions (operations), we call the inherited class A as a base class, a parent class, or a superclass, whereas inheriting Class B is a derived class or subclass of a.

Represents the terms of the parent and child classes: Parent and subclass, superclass and subclass, base class, and derived class, they represent the same meaning.

Why you need to inherit

The development of animal species, in which animals are penguins and mice, are required as follows:
Penguin: Attribute (name, id), method (eat, sleep, introduce yourself)
Mouse: Attribute (name, id), method (eat, sleep, introduce yourself)
Penguins and mice are animals. We can not write an animal class, so the code is not a lot of concise. Some people say I would like to build two classes alone write this property, I can only tell you can, but it is like, your father gave you millions of, you do not, to throw the money to earn their own, you want to do so I have no way. Since Java provides us with inheritance we need to use this will greatly improve our development efficiency, such as: Improved maintainability, code is more concise, improve the reusability of the code is also improved (reusability refers to the use of multiple, no longer write the same code).

Role:

1, inheritance can reduce the duplication of code. For example, the parent class has provided the method, the subclass can be used directly, no longer need to implement.

2, inheritance is the precondition of polymorphism. Of course, the use of inheritance also increases the coupling degree of the class.

  When you do not need the properties of the parent class, you can override the tuning property.

Python inheritance

Inheritance is divided into multiple inheritance and single inheritance, while Python is a multi-inheritance. Python is a little different from Java.

Characteristics:

? 1: inherited syntax class derived class name ( base class name )://... base class name in parentheses, the basic class is defined in the class, in the tuple, the base class of the new class has an object by default, while the base class is more than the right multiple inheritance

? 2: The construction of the base class in inheritance (Theinit() method) is not automatically called, it needs to be called specifically in the construction of its derived class. Unlike Java,java, the initialization method is called automatically.

3: When calling a method of the base class, you need to prefix the class name of the base class with the Self argument variable. Unlike calling a normal function in a class, you do not need to take the self argument

4:python always looks for a method of the corresponding type first, and if it cannot find the corresponding method in the derived class, it begins to look in the base class one by one. (Find the method that was called in this class before you can find it in the base class).

Initialization

The first method: the base class name. __init__ (Self,arg)

classAObject):# New Class    "" " base class " ""    def __init__( Self, name, age):# super (A, self). __init__ ()         Self. Name=Name Self. Age=AgeclassBObject):"" " base class " ""    def __init__( Self, height): Self. Height=HeightclassC (A):"" inherits one, single inheritance " " "    def __init__( Self, name, age, Sex): A.__init__( Self, name, age)#第一种方法         Self. Sex=SexclassD (A, B):"" inherits one, single inheritance " " "    def __init__( Self, name, age, Sex,height): A.__init__( Self, name, age)#第一种方法B.__init__( Self, height) Self. Sex=Sex

The second method: The Supper keyword is supper (obj,self). __init__ (ARG)

classAObject):# New Class    "" " base class " ""    def __init__( Self, name, age):SuperA Self).__init__() Self. Name=Name Self. Age=AgeclassBObject):"" " base class " ""    def __init__( Self, height): Self. Height=HeightclassC (A):"" inherits one, single inheritance " " "    def __init__( Self, name, age, Sex):SuperC Self).__init__(name, age)#第二种方法         Self. Sex=SexclassD (A, B):"" inherits one, single inheritance " " "    def __init__( Self, name, age, Sex,height):SuperD Self).__init__(name, age)#第二种方法        # only the first base class can be initialized with this method, and the second base class cannot be initialized if there are different parameters, it is recommended to use the first one         Self. Sex=Sex

Note: the second type is recommended for single inheritance, and multiple inheritance uses the first

The purpose of multiple inheritance is to select and inherit subclasses from the two inheritance trees, so that the composition function is used. Java's multiple inheritance is implemented by implementing multiple interfaces.

Isinstance (A, B)

You can determine the type of a variable that can be used to determine whether a is an instance object of B, return True or False

Type ()

function gets the type of the variable, returns the type of the variable

Python Object-oriented (iii) 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.