Class Definition, inheritance, and object instance usage in Python

Source: Internet
Author: User

Class Definition, inheritance, and object instance usage in Python

This article mainly introduces the definition, inheritance, and usage objects of classes in Python. It analyzes the related concepts and usage skills of classes in Python in detail and has some reference value, for more information, see

This document describes how to define, inherit, and use classes in Python. Share it with you for your reference. The specific analysis is as follows:

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:

For example, the concept of a class:

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

Example 1 (class definition ):

?

1

2

3

4

5

6

7

>>> Class pp:

... Pass

...

>>> P = pp ()

>>> Print p

<__ Main _. pp instance at 0x00CA77B0>

>>>

Print the type of this variable. It tells us that we already have an instance of the Person class in the _ main _ module.

Example code 2 (_ init _ usage ):

Note: __init _ method runs immediately when the class object is created. This method is used to initialize the object.

?

1

2

3

4

5

6

7

8

9

10

>>> Class Person:

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

... Self. name = name

... Def sayHi (self ):

... Print 'hello, my name is ', self. name

...

>>> P = Person ('swaroop ')

>>> P. sayHi ()

Hello, my name is Swaroop

>>>

Example 3 (_ del ):

Note: __del _ is called when the program exits.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

>>> Class Person:

... Population = 0

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

... Self. name = name

... Print '(Initializing % s)' % self. name

... Def _ del _ (self ):

... Print '% s says bye.' % self. name

... Person. population-= 1

...

... Def howMany (self ):

... If Person. population = 1:

... Print 'I am the only person here .'

... Else:

... Print 'we have % d persons here. '% Person. population

...

>>> A = Person ('A ')

(Initializing aa)

>>> A. howMany ()

We have 0 persons here.

>>> B = Person ('bb ')

(Initializing bb)

>>> B. howMany ()

We have 0 persons here.

>>> ^ Z

Aa says bye.

Bb says bye.

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.

For example, class definition and usage:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

Class CAnimal:

Name = 'unname'

# Member variables

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

# Overload Constructors

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.

For example, class inheritance

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

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 a function to the subclass.

SetVoice 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

I hope this article will help you with Python programming.

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.