Class (1) and Python class (

Source: Internet
Author: User

Class (1) and Python class (
Class (1) in Python I. Application scenarios

If multiple functions have the same parameters, convert them to object-oriented.

Ii. How to Create a class

Class is a set of objects with the same attributes and methods. It defines the attributes and Methods shared by each object in the set. The object is a class instance.

Class Name:

Pass

Iii. class variables

Class variables are public in the entire instantiated object. Class variables are defined in the class and outside the function body. Class variables are generally not used as instance variables.

4. Create methods in the class

A method is a function defined in a class.

1. Common Law

Obj = Class Name ()

Obj. Common method name

2. Set initialization (constructor and encapsulation features)

Because the class can act as a template, You can forcibly enter some attributes that we think must be bound when creating an instance. By defining a special _ init _ method, you can bind attributes such as name and score when creating an instance:

Class Student (object ):

Def _ init _ (self, name, score ):

Self. name = name

Self. score = score

Note: There are two underscores before and after the special method "init !!!

Note that the first parameter of the _ init _ method is always self, which indicates the created instance itself. Therefore, within the _ init _ method, you can bind various attributes to self, because self points to the created instance itself.

With the _ init _ method, you cannot input an empty parameter when creating an instance. You must input a parameter that matches the _ init _ method, but self does not need to be passed. The Python interpreter will pass the instance variables in:

>>> Bart = Student ('bart Simpson ', 59)

>>> Bart. name

'Bart Simpson'

>>> Bart. score

59

Compared with a common function, the function defined in the class is only a little different, that is, the first parameter is always the instance variable self, and you do not need to pass this parameter when calling. In addition, class methods are no different from common functions. Therefore, you can still use default parameters, variable parameters, keyword parameters, and named keyword parameters.

V. inheritance features

Inheritance is used to use methods in the parent class.

Create an instance:

Class Father (): # parent class

Def f1 (self ):

Print ('parent method 1 ')

Class Son (Father): # subclass

Def s1 (self ):

Print ('method 1 ')

Call:

  • Obj = Son ()
  • Obj. s1 ()
  • Obj. f1 ()

# Execution result: Sub-method 1

Parent method 1

When the method name in the subclass is the same as the method name in the parent class (override a method in the parent class), the method in the subclass is executed when the method is called. You can refer to the following methods when you want to execute the parent class method during rewriting.

Class Father (): # parent class

Def f1 (self ):

Print ('parent method 1 ')

Class Son (Father): # subclass

Def s1 (self ):

Super (Son, self). f1 # parameter: subclass name, self

Print ('method 1 ')

# Call

  • Obj = Son ()
  • Obj. s1 ()

# Execution result: parent method 1

Sub-method 1

You can also write the statement as follows:

Class Father (): # parent class

Def f1 (self ):

Print ('parent method 1 ')

Class Son (Father): # subclass

Def s1 (self ):

Father. f1 (self)

Print ('method 1 ')

# Call

  • Obj = Son ()
  • Obj. s1 ()

Multi-inheritance:

When you need a subclass to inherit multiple parent classes, you can refer to the following code:

Class Father1 (): # parent class 1

Def f1 (self ):

Print ('parent class 1 ')

Class Father2 (): # parent class 2

Def f2 (self ):

Print ('parent class 2 ')

Class Son (Father1, fatsp2): # subclass

Def s1 (self ):

Print ('method 1 ')

The order of inheriting the parent class is from left to right. That is, when multiple inherited parent classes have methods with the same name, the method of the Left parent class will be executed during the call, while the method of the right parent class will not be executed. This is related to the execution sequence. When a subclass is called, the program will first match from the subclass method. If the subclass does not exist, it will match in sequence in the delimiter class. The matching sequence of the parent class is from left to right.

When multiple parent classes inherit from one ancestor class and the called methods are in the ancestor class, the query call sequence is shown in.

When other methods are called in the method that calls the parent class, the method is matched from the subclass. Even if this method exists in the original parent class, it will be searched from the subclass.

Related Article

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.