The object-oriented: acquaintance

An Object Oriented acquaintance

1.1 Review of process-oriented programming vs functional programming

Copy the code
# Procedural programming element number of the measurement object-oriented all. 
= S1 'fjdsklafsjda' 
COUNT = 0 
for I in S1: 
    COUNT +. 1 = 


L1 = [1,2,3,4] 
COUNT = 0 
for I in L1: 
    COUNT = +. 1
Copy the code
Copy the code
def func(s):
    count = 0
    for i in s:
        count += 1
    return count
func('fdsafdsa')
func([1,2,3,4])
Copy the code

By contrast found: process-oriented programming function programming than the most significant two characteristics:

1, reducing the reusability of code.

2, enhance the readability of the code.

1.2 vs functional programming object-oriented programming

Copy the code
# Functional programming 

# auth authentication-related 
DEF the Login (): 
    Pass 

DEF regisgter (): 
    Pass 

# the Account account relevant 
DEF func1 (): 
    Pass 

DEF func2 (): 
    Pass 


# cart-related 
DEF Shopping (username, Money): 
    Pass 
DEF check_paidgoods (username, Money): 
    Pass 
DEF check_unpaidgoods (username, Money): 
    Pass 
DEF Save (username, Money): 
    Pass
Copy the code
Copy the code
class LoginHandler:
    def login(self):
        pass

    def regisgter(self):
        pass

class Account:
    def func1(self):
        pass

    def func2(self):
        pass

class ShoppingCar:
    def shopping(username,money):
        pass
    def check_paidgoods(username,money):
        pass
    def check_unpaidgoods(username,money):
        pass
    def save(username,money):
        pass
Copy the code

# Can be seen by comparing the first object-oriented advantages:

Object-oriented programming: is a collection of similar type functions, a make your code more clarity, more reasonable.

He said second advantage before, take a look at what is object-oriented.

Object-oriented programming is the core of the object (God thinking), to understand why the object must regard themselves as God, all things are all objects exist in the eyes of the world God does not exist can be created.

What is the class? What is an object?

Class: a class of things that have the same properties and functions.

Object: is a concrete manifestation of the class.

Specifically some of them: first explain explain what ⻋ has wheels, there is the direction that the discs, engine, will run is ⻋ good in explaining ⼀ What is the Face has a name, age, hobbies, will sing and dance thinking?.... Face is so broadly on the train, people are like: but my specific car, you are the person that is an object.

Cat, is a class, you big orange domesticated.

Dog is a class next door domesticated The two - Kazakhstan is the object.

⾯ to an object thought to be Your Own create an object. Your Own Create the scene you are to the object world is ⾯ of God. Do you want ⻋, dry, dry thing on the thing. You want to be able to Face Face, dry, dry thing thing.

Besides second advantage: object-oriented, to have God's perspective look at the problem, in fact, a common class template (plant), the object is instantiated from specific templates out (slowly come to realize).

1.3 Structure of the class

Copy the code
Human class: 
    "" " 
    Such constructs mainly human 
    " "" 
    Mind = 'thinking' first part #: Static Properties Static variable static field 
    DIC} = { 
    L1 = [] 
    DEF Work (Self): # a second portion : function method dynamic properties 
        print ( 'work humans') 
class use the same keyword def, define a class. 
Human is a kind of class names, class names use camel (CamelCase) naming style, capitalized private classes available a leading underscore. 
Class structure of the general direction to be divided into two parts: the 
static variable. 
Dynamic method.
Copy the code

II. From the perspective of the class name class research

2.1 class name of the operating static properties

   2.11 First, view the contents of all classes: Class name .__ dict__ way.

Copy the code
Human class: 
    "" " 
    Such constructs mainly human 
    " "" 
    Mind = 'thinking' first part #: Static Properties Static variable static field 
    DIC} = { 
    L1 = [] 
    DEF Work (Self): # a second portion : method function dynamic properties 
        # Print (Self) 
        Print ( 'humans working') 

Print (human .__ dict__ magic) 
Print (human .__ dict __ [ 'Mind']) 
human .__ dict __ [ 'Mind'] = 'no brain' 
Print ( Human .__ dict__) # error 
# only query this way, not additions and deletions. 

# The first way only the entire contents of the user's query (general query without a separate property).
Copy the code

  2.12 The second: universal point.  

Copy the code
Human class: 
    "" " 
    Such constructs mainly human 
    " "" 
    Mind = 'thinking' first part #: Static Properties Static variable static field 
    DIC} = { 
    L1 = [] 
    DEF Work (Self): # a second portion : The method of dynamic properties function 
        # Print (Self) 
        Print ( 'work humans') 
Print (Human.mind) # check 
Human.mind = 'no brain' # change 
Print (Human.mind) 
del Human.mind # puncturing 
human. walk = 'upright' 
Print (Human.walk) 
# can add or delete a single attribute class change search point by universal
Copy the code

  Make a summary of the above two: If you want all the contents of the query class, __dict__ by the first method, if only a single operating point is a universal property of ways.

2.2 A method of operating a dynamic class name

  Provided: In addition to two special methods: Method to static class method, the method generally does not operate by the class name of a class.

Copy the code
Human class: 
    "" " 
    Such constructs mainly human 
    " "" 
    Mind = 'thinking' first part #: Static Properties Static variable static field 
    DIC} = { 
    L1 = [] 
    DEF Work (Self): # a second portion : The method of dynamic properties function 
        # Print (Self) 
        Print ( 'work humans') 
    DEF tools (Self): 
        Print ( 'human use tools') 

Human.work (111) 
Human.tools (111) 
below may be done, but No need to. 
Human .__ dict __ [ 'work' ] (111)
Copy the code

III. Research from the perspective of the object class

3.1 What is the object

The object is out of the class, as long as the class name with (), which is an example of the process, this will instantiate an object.

Execute the following code what will happen?

Copy the code
class Human: 
    Mind = 'thinking' 
    DEF __init __ (Self): 
        Print (666) 
        Print (Self) # <__ main __ Human Object AT 0x00000191508AA828.> 

    DEF Work (Self): 
        Print ( 'humans work') 

    DEF Tools (Self ): 
        Print ( 'human use tools') 
obj = human () # Whenever an instance of an object, it automatically performs __init__ method 
Print (obj) # <. __ main __ human Object AT 0x00000191508AA828> 
# and obj address and self the same address
Copy the code

In fact, instantiate an object total, there were three things:

  1, opened up an object space in memory.

  2, class __init__ method automatically executed, and the target space (memory address) passed __init__ method first position parameter self.

  3, in the __init__ method add attributes to the object space through the self.

Example:

Copy the code
class Human: 
    Mind = 'thinking' 
    Language = 'language' 
    DEF __init __ (self, name, Sex, Age, Hobby): 
        # self and obj points to the same memory address in the same space, here is through self to this four properties target space of the package. 
        name = self.n 
        self.s Sex = 
        self.a = Age 
        self.h = Hobby 

obj = Human ( 'Barry', 'M', 18, 'motion')
Copy the code

Object attributes operation target space 3.2

  3.21 object query object all properties. Object .__ dict__

Copy the code
class Human: 

    Mind = 'thinking' 
    Language = 'practical language' 
    DEF __init __ (self, name, Sex, Age, Hobby): 
        # self and obj points to the same memory address in the same space, here is through self to this four properties target space of the package. 
        name = self.n 
        self.s Sex = 
        self.a = Age 
        self.h = Hobby 

obj = Human ( 'Barry', 'M', 18, 'motion') 
Print (obj .__ dict__ magic) # { 'n-': 'barry', 'h': ' motion', 's': 'M', 'a': 18}
Copy the code

  3.22 object operations single attribute object. Omnipotent point.

Copy the code
class Human: 

    Mind = 'thinking' 
    Language = 'practical language' 
    DEF __init __ (self, name, Sex, Age, Hobby): 
        # self and obj points to the same memory address in the same space, here is through self to this four properties target space of the package. 
        name = self.n 
        self.s Sex = 
        self.a = Age 
        self.h = Hobby 

obj = Human ( 'Barry', 'M', 18, 'motion') 
obj.job = 'the IT' # by 
del obj. # n-puncturing 
obj.s = 'M' # change 
print (obj.s) # check 
print (obj .__ dict__)
Copy the code

3.3 Object class attributes See

Copy the code
Human class: 

    Mind = 'thinking' 
    Language = 'practical language' 
    DEF __init __ (Self, name, Sex, Age, Hobby): 
        self.n = name 
        self.s = Sex 
        self.a = Age 
        self.h = Hobby 

obj = Human ( 'barry', 'M', 18, 'motion') 
Print (obj.mind) 
Print (obj.language) 
obj.a = 666 
Print (obj.a)
Copy the code

3.4 Method class object manipulation

Copy the code
Human class: 

    Mind = 'thinking' 
    Language = 'practical language' 
    DEF __init __ (Self, name, Sex, Age, Hobby): 
        self.n = name 
        self.s = Sex 
        self.a = Age 
        self.h = Hobby 

    DEF work (Self): 
        Print (Self) 
        Print ( 'humans work') 

    DEF tools (Self): 
        Print ( 'humans use tools') 

obj = human ( 'Barry', 'male', 18, 'movement') 
obj.work () 
obj.tools ()
Copy the code

  The method generally by class (class methods out of the outer, static methods) on the object, and the object implementation of these methods are self parameter automatically passed to the method in object space.

What self is?

in fact, self class method (function) in the first position parameter, but the interpreter will automatically call this function of the object pass self. So we first argument of the class convention provided self, is representative of this object.

A plurality of class objects can be instantiated

obj1 = Human ( 'chubby', 'M', 20 'beauty') 
obj2 = Human ( 'Xiangye', 'M', 18, 'fat women') 
Print (OBJ1, obj2) 
Print (OBJ1 .__ dict__ magic ) 
Print (obj2 .__ dict__)

Guess you like

Origin www.cnblogs.com/saoqiang/p/11297385.html