Python learning: Concepts, definitions, and usage of class classes

Source: Internet
Author: User

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: Concepts of Classes
Class Humans:
Name = ' Not named ' # Member variables
Def Speech (content ): # Member Functions
Print Content # Member variable assigns initial value to someone = human () # defines a human object to someone
Someone. Name = " Lu renjia "
Someone. Speak ( ' Hello everyone ' ) # Lu renjia speaks
>>> 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 usage
Class Canimal:
Name = ' Unname ' # Member variable def _ init _ (self, voice = 'hello'): # overload Constructor
Self. Voice = voice # Create a member variable and assign an initial value
Def _ Del __ (Self ): # Overload destructor
Pass # Null operation
Def Say (Self ):
Print Self. Voice
T = canimal () # Define animal object t
T. Say () # T speak
> Hello # Output
Dog = canimal ( ' Wow ' ) # Define the animal object dog
Dog. Say () # Dog talk
> 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
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)
Class Cdog (canimal ): # Inheritance class canimal
Def Setvoice (self, voice ): # Added 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.