Python class definition, inheritance and class object usage method concise tutorial, python concise tutorial

Source: Internet
Author: User

Python class definition, inheritance and class object usage method concise tutorial, python concise tutorial

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:

# Example: class concept class human: Name = 'unname' # member variable def speak (content): # member function print content # member variable assign initial value to someone = human () # define a human object to someone. name = "Lu renjia" someone. speaking ('Hello all ') # speaking with Lu renjia> 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.

# Example: class definition and use class CAnimal: name = 'unname' # member variable def _ init _ (self, voice = 'hello'): # overload constructor self. voice = voice # create a member variable and assign the initial value def _ del _ (self): # reload the Destructor pass # Empty operation def Say (self): print self. voicet = CAnimal () # define the object tt. say () # t speak> hello # output dog = CAnimal ('wow') # define the animal object dogdog. 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.

# Example: class inheritance 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 # null operation statement (no operation is performed) class CDog (CAnimal): # inheritance class CAnimal def SetVoice (self, voice ): # 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 hellobobo. 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.