Simple tutorial on defining, inheriting, and using Python classes

Source: Internet
Author: User

Simple tutorial on defining, inheriting, and using Python classes

This article describes the definition, inheritance, and usage of class objects in Python. This article describes the definition, inheritance, and usage of class objects in easy-to-understand languages, very practical and easy to understand. For more information, see

The concept of classes in Python programming can be compared to the description of a certain type set. For example, "human" can be considered as a class, then we use the human class to define every specific person-you, me, and others as their objects. The class also has attributes and functions. attributes are some characteristics of the class, such as human attributes such as name, height, and weight. The specific values are different for each person; A function is a behavior that can be implemented by a class, such as eating, walking, and sleeping. The specific form is as follows:

?

1

2

3

4

5

6

7

8

9

10

# Example: class concept

Class human:

Name = 'unname' # member variable

Def speak (content): # member functions

Print content # assign initial values to member variables

 

Someone = human () # define a human object to someone

Someone. Name = "Lu Ren jia"

Someone. Speak ('Hello all ') # LU renjia speak

>>> Hello, everyone! # Output

In Python, classes are defined and used in the form of class names [(parent class name)]: [member functions and member variables]. class names are the names of these classes, the parent class name is optional, but after the parent class name is defined, the Child class has the corresponding attributes and methods of the parent class. When defining an object as a class, the _ init _ constructor is called first to initialize the attributes of the object and the attributes of the class (member variables) you can define them in the constructor. You only need to add the object pointer to the definition. When an object is destroyed, the _ del _ destructor is called. when defining a class member function, a variable must be used by default (similar to the this pointer in C ++) represents the object defined by the class. The variable name can be customized. The following example uses the self variable to represent the class object variable.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

# Example: Class Definition and usage

Class CAnimal:

Name = 'unname' # member variable

Def _ init _ (self, voice = 'hello'): # reload Constructor

Self. voice = voice # create a member variable and assign an initial value

Def _ del _ (self): # reload destructor

Pass # null operation

Def Say (self ):

Print self. voice

 

T = CAnimal () # define an animal object t

T. Say () # t speak

> Hello # output

Dog = CAnimal ('wow') # define the animal object dog

Dog. Say () # dog speak

> Wow # output

In Python programming, classes can inherit attributes of the class, in the form of class name (parent class). subclasses can inherit all methods and attributes of the parent class, you can also overload the member functions and attributes of the parent class. Note that if the member functions of the Child class are overloaded with the same name, the member functions of the Child class are used.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

# Example: class inheritance

Class CAnimal:

Def _ init _ (self, voice = 'hello'): # The default value for voice Initialization is hello.

Self. voice = voice

Def Say (self ):

Print self. voice

Def Run (self ):

Pass # null operation statement (no operation is performed)

 

Class CDog (CAnimal): # inheritance class CAnimal

Def SetVoice (self, voice): # Add the SetVoice function to the subclass.

Self. voice = voice

Def Run (self, voice): # subclass overload function Run

Print 'running'

 

Bobo = CDog ()

Bobo. SetVoice ('My Name is BoBo! ') # Set child. data to hello.

Bobo. Say ()

Bobo. Run ()

 

> My Name is BoBo!

> Running

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.