Object-oriented: acquaintance

An Object Oriented acquaintance

1.1 Review of process-oriented programming vs functional programming

Number of elements in the measurement object-oriented programming process all.

s1 = 'fjdsklafsjda'
count = 0
for i in s1:
count += 1

l1 = [1,2,3,4]
count = 0
for i in l1:
count += 1

def func(s):
count = 0
for i in s:
count += 1
return count
func('fdsafdsa')
func([1,2,3,4])

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

Functional Programming
Object-oriented programming

As 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.

The second advantage: the object-oriented, to have God's perspective look at the problem, in fact, a common class template (plant), the object is instantiated from a particular template.

1.3 Structure of the class

class Human:
"""
此类主要是构建人类
"""
mind = '有思想' # 第一部分:静态属性 属性 静态变量 静态字段
dic = {}
l1 = []
def work(self): # 第二部分:方法 函数 动态属性
print('人类会工作')

class is the same keyword def usage, 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.

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.
class Human:
"""
此类主要是构建人类
"""
mind = '有思想' # 第一部分:静态属性 属性 静态变量 静态字段
dic = {}
l1 = []
def work(self): # 第二部分:方法 函数 动态属性
\# print(self)
print('人类会工作')

print(Human.__dict__)
print(Human.__dict__['mind'])
Human.__dict__['mind'] = '无脑'
print(Human.__dict__) # 错误

In this way only query, not additions and deletions.

The first way only the entire contents of the user's query (usually without a separate attribute query)

2.12 The second: universal point.

class Human:
"""
此类主要是构建人类
"""
mind = '有思想' # 第一部分:静态属性 属性 静态变量 静态字段
dic = {}
l1 = []
def work(self): # 第二部分:方法 函数 动态属性
\# print(self)
print('人类会工作')
print(Human.mind) # 查
Human.mind = '无脑' # 改
print(Human.mind)
del Human.mind # 删
Human.walk = '直立行走'
print(Human.walk)

You can add or delete a single attribute class change search point by universal

  ** The above two make a conclusion: 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.

class Human:
"""
此类主要是构建人类
"""
mind = '有思想' # 第一部分:静态属性 属性 静态变量 静态字段
dic = {}
l1 = []
def work(self): # 第二部分:方法 函数 动态属性
# print(self)
print('人类会工作')
def tools(self):
print('人类会使用工具')

Human.work(111)
Human.tools(111)
下面可以做,但不用。
Human.__dict__['work'](

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?

class Human:
mind = '有思想'
def **init**(self):
print(666)
print(self) # <**main**.Human object at 0x00000191508AA828>

def work(self): 
    print('人类会工作')

def tools(self):
    print('人类会使用工具')
    
obj = Human() # 只要实例化对象,它会自动执行__init__方法
print(obj) # <**main**.Human object at 0x00000191508AA828>

And the self address as that obj is the same

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:

class Human:
mind = '有思想'
language = '使用语言'
def **init**(self,name,sex,age,hobby):
\# self 和 obj 指向的是同一个内存地址同一个空间,下面就是通过self给这个对象空间封装四个属性。
self.n = name
self.s = sex
self.a = age
self.h = hobby

obj = Human('barry','男',18,'运动')

Object attributes operation target space 3.2

3.21 object query object all properties. Object .__ dict__

class Human:
mind = '有思想'
language = '实用语言'
def __init__(self,name,sex,age,hobby):
    # self 和 obj 指向的是同一个内存地址同一个空间,下面就是通过self给这个对象空间封装四个属性。
    self.n = name
    self.s = sex
    self.a = age
    self.h = hobby
obj = Human('barry','男',18,'运动')
print(obj.__dict__) # {'n': 'barry', 'h': '运动', 's': '男', 'a': 18}

3.22 object operations single attribute object. Omnipotent point.

class Human:
mind = '有思想'
language = '实用语言'
def __init__(self,name,sex,age,hobby):
    # self 和 obj 指向的是同一个内存地址同一个空间,下面就是通过self给这个对象空间封装四个属性。
    self.n = name
    self.s = sex
    self.a = age
    self.h = hobby
    obj = Human('barry','男',18,'运动')
obj.job = 'IT' # 增
del obj.n # 删
obj.s = '女' # 改
print(obj.s) # 查
print(obj.__dict__)

3.3 Object class attributes See

class Human:
mind = '有思想'
language = '实用语言'
def __init__(self,name,sex,age,hobby):
    self.n = name
    self.s = sex
    self.a = age
    self.h = hobby
obj = Human('barry','男',18,'运动')
print(obj.mind)
print(obj.language)
obj.a = 666
print(obj.a)

3.4 Method class object manipulation

class Human:
mind = '有思想'
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('人类会工作')

def tools(self):
    print('人类会使用工具')

obj = Human('barry','男',18,'运动')
obj.work()
obj.tools()

  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('小胖','男',20,'美女')
obj2= Human('相爷','男',18,'肥女')
print(obj1,obj2)
print(obj1.__dict__)
print(obj2.__dict__)

Guess you like

Origin www.cnblogs.com/qidaii/p/11373511.html