Python Basics-Object oriented

Source: Internet
Author: User

Class: The abstraction of a class of things, such as a car

Object: An instance of a class, such as a real BMW

Classes: Class keyword

Class Composition: Method \ class variable \ member variable \ local variable

Method: It can also be called a function, but unlike a function, the class method must contain the argument self, and the first argument

Class variable characteristics: defined in the class method, can be called directly by the class name, or you can have an object to invoke

Member variable characteristics: Called by the object of the class, usually self. The beginning of the definition. can be used in different methods of the class

Local variable: A normal variable in a method that can only be used within the method

Construction method and destructor method

The __init__ () initialization operation, which is automatically called once each time a new object is generated. Optional

__DEL__ () deletes the cleanup operation. Optional

In general, Python's garbage collection mechanism is handled very well, so it is generally not necessary to use the __del__ () method

classUser ():#definition of ClassAge = 20#class variables, defined outside of the class method        #init can be selected, have initialization to write, no initialization can not write    " "Why must I include the formal parameter self in the method definition?        Because Python calls this __init__ () method to create a user instance, the argument self is automatically passed in.    Each method call associated with a class automatically passes the argument self, which is a reference to the instance itself, allowing the instance to access the properties and methods in the class. " "        def __init__(self):#Construction MethodSelf.name="xiaoming"#member Variables        Print("name=", Self.name)defSay (self):#The class method must contain the parameter self, and the first argumentAddress =" China"#local variables in the method        Print("address=", address) self.address= Address#If you want variables to be used among the methods in your class, you need to do this            defCry (self):#print (address) #报错        Print(self.address)#corresponds to the aboveu= User ()#U is the object of the classPrint("age=", U.age)#Calling class variablesU.say () u.cry ( )

Results:

Name= xiaoming
age= 20
Address= China
China

Static method: parameter can be empty

classUser1 (): Name_count= 0#class Variables        def __init__(self,name): Self.name=name User1.name_count+ = 1Print(Name +", number of statistics:", User1.name_count)defSay (self):Print("I was"+self.name)#static method, parameter can be null@staticmethoddeffunc1 ():Print("I'm a static method .") U1= User1 ("Xiao Ming No. 1th") U1.say () U2= User1 ("Xiao Ming No. 2nd") U2.say () user1.func1 ()

Results:

Xiao Ming No. 1th, Count: 1
I'm Xiao Ming, number 1th.
Xiao Ming No. 2nd, Count: 2
I'm Xiao Ming, number 2nd.
I'm a static method.

Practice:

Define an employee class employee
Requires the name and salary of the employee to be initialized when the class is created
There are two general methods in the class, one to print the number of employees and one to print the employee's information (name and salary)

classEmployee:count=0def __init__(self,name,salary): Self.name=name Self.salary=Salary Employee.count+=1#The use of class variables in methods            defPrint_employee_number (self):Print("Number of employees:", Employee.count)defPrint_employee_xinxi (self):Print(self.name,self.salary) employee1= Employee ("xiaoming", 5000) Employee1.print_employee_number () Employee1.print_employee_xinxi () Employee2= Employee ("xiaoming1", 6000) Employee2.print_employee_number () Employee2.print_employee_xinxi ()

Results:

Number of employees: 1
Xiaoming 5000
Number of employees: 2
XIAOMING1 6000

Python Basics-Object oriented

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.