Definition and inheritance of classes in Python and the use of object instances

Source: Internet
Author: User
Tags class definition constructor inheritance

This article mainly introduces the definition, inheritance and use object of the class in Python, and analyzes the related concepts and using techniques of Python in detail, which has some reference value, and the friends who need it can refer to the following

The examples in this article describe the definition, inheritance, and methods of using objects in Python. Share to everyone for your reference. The specific analysis is as follows:

The concept of classes in Python programming can be likened to a description of a set of types, such as "humans" can be viewed as a class, and then the human class defines each specific person-you, me, and him as its object. A class also has attributes and functions, attributes that are attributes of the class itself, such as human names, height, and weight, while the specific values vary according to each person, and function is what the class can do, such as human beings with functions such as eating, walking and sleeping. The specific forms are as follows:

Example: The concept of a class:

Class Human:

name = ' Unnamed ' # member variable

def talking (content): # member function

Print Content # member variable assignment initial value

someone = Human () # define a Human object someone

SB. Name = "Passerby"

SB. Speak (' Everyone's good ') # speak to a passerby

>>> Hello, everyone! # output

Sample program one (Definition of Class):

?

1 2 3 4 5 6 7 >>> class pp: ... >>> p = pp () >>> print P <__main__.pp instance at 0x00ca77b0> >>>

The type of the variable is printed. It tells us that we already have an instance of the person class in the __main__ module.

Sample Program II (__INIT__ usage):

Description: The __init__ method runs immediately when the object of the class is set up. 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 NA Me is ', self.name ... >>> p = person (' Swaroop ') >>> p.sayhi () Hello, I name is Swaroop >>>

Sample program Three (__del__ method):

Description: The __del__ method is invoked 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 The >>> class Person: ... population = 0. def __init__ (SE LF, 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 (' AA ') (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.

A class is defined and used in Python in the form of: Class name [(parent class name)]:[member function and member variable], class name is the name of this class, and the parent class name is optional, but after the parent class name is defined, the subclass has the corresponding properties and methods of the parent class. When a class is defined as an object, the __init__ constructor is called first to initialize the properties of the object, and the properties of the class (member variables) can be defined in the constructor, as long as the object pointer is added to the definition. When an object is destroyed, the __del__ destructor is invoked, and when the member function of the class is defined, a variable (similar to the this pointer in C + +) must be used to represent the object itself defined by the class, and the name of the variable can be defined by itself.

Example: class definition and use:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17-18 Class Canimal:name = ' unname ' # member variable def __init__ (self,voice= ' Hello '): # overloaded constructor Self.voice = Voice # Create a member variable and assign an initial value def __de L__ (self): # overload destructor Pass # NULL operation def Say (self): print Self.voice t = canimal () # define animal Objects T T.say () # t speak >> Hello # output D og = canimal (' wow ') # defines animal objects dog dog. Say () # Dog speak >> Wow # output

In Python programming, classes can inherit the parent class attribute, the class name (the parent class), the subclass can inherit all the methods and properties of the parent class, or the member functions and properties of the parent class, and note that the subclass member function uses the subclass member function if it overloads the parent class (that is, the same name)

Example: Inheritance of Classes

?

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 '): # Voice initialization defaults to Hello Self.voice = Voice def Say (self): print Self.voice def Run (self): Pass # Empty Action statement (no Action) class Cdog (Canimal): # inheriting class Canimal def setvoice (Self,voice): # Subclass Add function Setvoice Self.voice = Voice def Run (Self,voice): # Subclass overloaded 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 your Python programming.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.