Difference between process oriented and object-oriented:
Process oriented things are relatively simple and can be solved by linear thinking. Object-oriented things are relatively complex and cannot be solved by simple linear thinking.
Common points of process oriented and object-oriented:
Both process oriented and object-oriented are a way of thinking to solve practical problems.
The two complement each other, not opposites. To solve complex problems, it is convenient for us to grasp the complex relationship between time from a macro perspective and analyze the whole system through object-oriented method; When it comes to micro operations, the process oriented approach is still used.
Category: category, classification, birds of a feather flock together, humans, birds, animals, plants
Class is the general name of a group composed of many similar things. It can help us quickly understand and judge the nature of things.
Data type: different data types belong to different classes
Viewing data types using built-in functions
Objects: for example, 100, 99520 are similar and different examples contained under the int class. This example is called an instance or object in professional terms.
Class creation:
Syntax for creating classes:
class Student: pass
Class composition:
Class properties
Example method
Static method
Class method
Object creation: also known as class instantiation
Syntax: instance name = class name ()
The code is as follows:
class Student: #Student is the class name, which is composed of one or more words. The first letter of each word is uppercase and the rest is lowercase native_plac = 'Zhanjiang' #Variables written directly in a class are called class attributes def __init__(self,name,age): self.name = name #self.name is called an entity method. It performs an assignment operation to assign the value of the name of the local variable to the entity attribute self.age = age #Example method def eat(self): print('Stir fry noodles on the roadside') #Static method @staticmethod def method(): print('I used it staticmethod So I'm a static method') #Class method @classmethod def method(cls): print('I used it classmethod So I'm a class method') #Functions defined outside a class are called functions, and methods defined within a class are called methods def drink(): print('Drinking draft beer on the street') #Is Student an object in Python? Is there any open space in the memory? #print(id(Student)) #print(type(Student)) #print(Student) #Create Student class object stu1 = Student('Zhang San',20) print(stu1) print(stu1.name,end='\t') stu1.eat() #Object name Method name () print(stu1.age) print('________---------------') Student.eat(stu1) #Class name Method name () 39 lines of code have the same function as 35 lines of code. They both call the eat method in Student
Operation method:
<__main__.Student object at 0x01E4EF90> Zhang San Stir fry noodles on the roadside 20 ________--------------- Stir fry noodles on the roadside Process finished with exit code 0
Class attribute, class method, static method:
Class attribute: variables outside methods in a class are called class attributes and are shared by all objects of the class
Class method: a method decorated with @ classmethod and accessed directly by class name
Static method: a method decorated with @ staticmethod and directly accessed by class name
The code is as follows:
class Student: #Student is the class name, which is composed of one or more words. The first letter of each word is uppercase and the rest is lowercase native_plac = 'Zhanjiang' #Variables written directly in a class are called class attributes def __init__(self,name,age): self.name = name #self.name is called an entity method. It performs an assignment operation to assign the value of the name of the local variable to the entity attribute self.age = age #Example method def eat(self): print('Stir fry noodles on the roadside') #Static method @staticmethod def method(): print('I used it staticmethod So I'm a static method') #Class method @classmethod def me(cls): print('I used it classmethod So I'm a class method') #Functions defined outside a class are called functions, and methods defined within a class are called methods def drink(): print('Drinking draft beer on the street') #Is Student an object in Python? Is there any open space in the memory? #print(id(Student)) #print(type(Student)) #print(Student) #Create Student class object stu1 = Student('Zhang San',20) stu2 = Student('Li Si',22) #How to use class properties print(Student.native_plac) print(stu1.native_plac) print(stu2.native_plac) Student.native_plac = 'Leizhou' print(stu1.native_plac) print(stu2.native_plac) print('-------------------Use of class methods----------------') Student.me() print('-------------------Use of static methods----------------') Student.method()
Operation results:
Zhanjiang Zhanjiang Zhanjiang Leizhou Leizhou -------------------Use of class methods---------------- I used it classmethod So I'm a class method -------------------Use of static methods---------------- I used it staticmethod So I'm a static method Process finished with exit code 0