An object-oriented basis

 

Today, content 


1, object-oriented concepts
2, the concept of classes and objects
3, how to create classes and objects
4, how to design attribute
5, look for a property of order
6, __ init__, class initialization method
7, binding and non-binding approach method

1, object-oriented concepts
of object-oriented: thinking is a programming

object-oriented three advantages
1. expansionary
2. flexibility
3. reusability
drawback:
the complexity of the program 1. improved
2. can not accurately predict the results of
the use of scene
of high scalability required procedures, usually directly to the user, for example: QQ, micro-channel

2, and the concept of object class
objects: the presence of a specific object, has certain features and acts
are characterized by a combination of skill and
class : is a collection of features and acts with the same object, is an abstract concept of

the relationship between objects and classes:
the class includes a series of objects
the object belongs to a class
1, is an example of class objects actually exist, i.e. Example
2, the object / instance only one action: attribute references
summarize a conclusion: when using the object-oriented programming, the first step is what objects need to think,
objects have what kind of features and behavior, which summed up the required type based on this information

3, how to create classes and objects
1, create a class
class class name:
DEF __init __ (Self, parameter 1, parameter 2):
. Self properties of an object parameter 1 = 1
Self attribute object parameter 2 = 2.

DEF method name (Self): Pass

DEF method name 2 (self): pass

the object class name name = (1,2) # is the object instance represents a specific thing (create objects)
# class name (): + class name in parentheses is an instance of a class, is equivalent to calling the __init__ method
# parentheses pass parameters, parameters do not need to pass self, others and the init parameter correspondence
# result returns an object
Object name. # 1 properties of the object to view the object's properties directly with the object name attribute to name
the object name. Method name () # call methods in the class, the direct use of the object name. Method name () can be
2, create object syntax:
class the Person:
Pass

# create objects
P = the Person ()

. 4, how to design property
in the object-oriented, the variable name is called attribute data, the name of the function called a constructor property
1, property class yes, yes there are all objects of class,
2, object attributes, the object is unique,
5, attribute search order
1, if the classes and objects have a common property, to own property to access the object, if there is no access class properties in
properties additions and deletions to
1, increase the property
the object variable name. attribute name = attribute value
2, modify
the object. property = new value
3, View property access to all properties of the object
print (object .__ dict__)
class information 4, to access the object
print (Object .__ class__ is)
. 6, __ method init__, class initialization
# init method
is called the initialization process is essentially a function of
characteristics 1: when the object is instantiated automatically when the init method of
Feature 2: automatically object as a first parameter passed in the name parameter bit self, self can be another name, but it is not recommended to change the

function: the user to set this object initial value of

7, binding and non-binding method method
1, object binding method (required to access the object itself the method is the object bound method)
class method which is the default method object binding
nature than when using the object that calls the function will automatically pass the object itself, as the first parameter

automatically when using the object that calls the function incoming object itself, as the first argument

when invoked with the class name he is an ordinary function, there are several parameters have to pass several parameters

in an object-oriented, the variable name is called data attributes, the name of the function the properties as a function of
why the objects bound: because the function inside To access the data in the object

2, class-bound method
class binding method @classmethod to decorate
special features: regardless of the call with a class or object will automatically incoming class itself, as the first argument
When binding to the object: When the function logic required to access the data object
when binding to the class: When the data logic function requires access to class

3. Non-binding method
is also called static methods, that is, that is not required accessing data type, you do not need to access the object's data
syntax: @staticmethod

 

Guess you like

Origin www.cnblogs.com/Fzhiyuan/p/11241065.html