Three Questions in Life
What is an object The real thing with certain characteristics and behaviors is the object, everything is the object. What is a class A combination of a series of objects with the same characteristics and behavior is called a class.In programming, only classes can have objects. 1. What is Object Oriented
Object-oriented and process-oriented are both a programming idea, one is process, the other is object, object-oriented is to treat everything as
Objects, we need to do things with objects to help us do them, not as process-oriented as we have to think about everything on our own
Things are the same.
2. Why use Object Oriented
Environments: Every company's business is growing, and we must have a scalable approach to this situation.
Advantages:
1. Extensibility.Everything is an object, and adding attributes to an object does not have a characteristic effect.
2. It simplifies the programmer's operation, so we don't have to go through things one step at a time, we just need to leave it to the object to complete it
Disadvantages:
1. Increased design complexity.We need to design the right objects beforehand to avoid over-design.
2. Program controllability is reduced.
3. How to use Object Oriented
Create Classes and Objects
# Define classes using class Class name:] Define a class # Specification for class names: Use hump nomenclature at the beginning of general capitalization class SHOldboyStudent: # Use variables to define the characteristics of class objects (often referred to as attributes) # Student Union Name, Age, Gender, School age = '11' name = 'egon' gender = 'female' school = 'HSOldboy' # Use functions to define the behavior of objects (often referred to as methods) # There can be multiple behaviors, and each time a behavior is defined, a parameter is automatically added self def say_hi(self): print('hi, i am boy!') def say_hello(self): print('hello, i am boy') # To create an object, you can use the Class Name()]To create an object student = SHOldboyStudent()
Properties of Classes
Notes: The object's namespace and the class's namespace are relatively independent, and public attributes are included in the class.Classes may not have attributes. Search order for attributes
Start with the namespace of the object===== class.
View properties (both object and class):
# To create an object, you can use the Class Name()]To create an object student = SHOldboyStudent() # View namespaces in classes print(SHOldboyStudent.name, SHOldboyStudent.__dict__) # View the object's namespace print(student.__dict__) # Access corresponding properties by object name print(student.name) # Result: # egon {'__module__': '__main__', 'age': '11', 'name': 'egon', 'gender': 'female', 'school': 'HSOldboy', 'say_hi': <function SHOldboyStudent.say_hi at 0x037E4F60>, 'say_hello': <function SHOldboyStudent.say_hello at 0x037F8078>, '__dict__': <attribute '__dict__' of 'SHOldboyStudent' objects>, '__weakref__': <attribute '__weakref__' of 'SHOldboyStudent' objects>, '__doc__': None} # {} # The object's namespace is empty # egon # But the object can access the value, why? Because the attributes in the class are the same for each object, there is no need to save two copies, just go to the class to find them when the object needs them.
Add attributes (objects only):
# Adding attributes only adds to objects, and attributes in classes do not change student = SHOldboyStudent() student.height = 15 print(student.height) # Give Object student Added a height attribute so you can access # print(SHOldboyStudent.height) This is an error, because there is no such property in the class
Delete attributes (objects only)
# Deleting attributes simply deletes attributes in the object, and attributes in the class do not change student = SHOldboyStudent() student.height = 15 print(student.height) # Give Object student Added a height attribute so you can access del student.height print(student.height) # You get an error when you go to view it, because at this point height Property has been deleted # print(SHOldboyStudent.height) This is an error, because there is no such property in the class
Change properties (object only)
# change attributes, Attributes in classes do not change student = SHOldboyStudent() print(student.name) # egon student.name = 'hu' print(student.name) # hu print(SHOldboyStudent.name) # egon
Method of class
Special method of class_u init_u
Specify a defined student class# Define classes using class Class name:] Define a class # Specification for class names: Use hump nomenclature at the beginning of general capitalization class SHOldboyStudent: # Use variables to define the characteristics of class objects (often referred to as attributes) # Student Union Name, Age, Gender, School age = '11' name = 'egon' gender = 'female' school = 'HSOldboy' # Use functions to define the behavior of objects (often referred to as methods) # There can be multiple behaviors, and each time a behavior is defined, a parameter is automatically added self def say_hi(self): print('hi, i am boy!') def say_hello(self): print('hello, i am boy')