Object-oriented programming design and development - Getting Started

The origin of the object-oriented

For more details, please see this article

What is object-oriented programming and why we need it

Process-oriented design

concept

The core process is the word, refers to the process steps to resolve the problem, which is to do and then doing ...... process-oriented design is like an assembly line better than well-designed, is a mechanical way of thinking. If the beginning of the program is to tackle a big problem, process-oriented basic design idea is this big problem into many small problems or sub-process, these sub-processes continue to break down in the process of implementation until small problems enough easy to solve in small steps within a range.

advantage

Process complexity of the problem, and further simplification (a complex issue, into a small step to achieve the achieve small steps will be very easy)

Shortcoming

A pipeline or process is used to solve a problem, producing soft drinks production of automotive assembly line can not, even can, they have a big change, change a component, indeed affect the whole body, scalability poor.

Scenarios

Process-oriented programming ideas are generally used for those functions once achieved rarely need to change the scene. If you just write a simple script, to do some one-time tasks, are excellent with a process-oriented way. Once the basic little change of scene, well-known examples are Linux kernel, git, and Apache HTTP Server, etc., but if you want to handle the task is complex and requires constant iteration and maintenance, and that was with the 面向对象most convenient.

Object-oriented programming

concept

The core is the object of the word, (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. Object-oriented programming such as good design Journey to the West, Tathagata problem to be solved is to pass through the book lands east Datang, Tathagata thought solve this problem requires four people: monk, Shahe Shang, pig, Monkey King, everyone has their own characteristics and skills (which is the object of the concept the features and skills correspond to data attributes and methods of properties of the object), but this is not fun, so Tathagata also arranged a group of demons, in order to prevent mentoring four Gaosi during the journey, and to arrange a group of gods escort, these are objects then begin to learn, mentoring four gods and demons interact with until finally made Scriptures. Tathagata would not pick up mentoring four tube in accordance with what the process), the object is a combination of characteristics and skills, based on Object-oriented design process is like creating a world, you are the God of this world, there are all of , There can also be created, in stark contrast to the thinking process-oriented mechanical, pay more attention to the object-oriented simulation of the real world, it is a "God" type of thinking.

advantage

Solve the problem of low expansion program. It is emphasized that, for a software quality, the object-oriented programming does not represent all of them. Object-oriented programming is only used to solve the scalability problem.

Software quality attributes

Shortcoming

1, much higher than the complexity of the programming process-oriented, object-oriented and do not understand it immediately started based on the design process, very prone to problems over-designed. Some low scalability requirements inviting scene using object-oriented programming will be more difficult, such as shell scripts linux management system is not suitable for use objects to design, process-oriented but more suitable.
2, can not be pipelined design can be very accurate prediction problem handling process and the results of the process-oriented programming, object-oriented programming once you start to solve the problems caused by the interaction between objects, even God can not accurately predict the final result . So we often see arcade game, a new game characters, Yin Pa skills very easily occur during the battle, the knife hacked to death three people, this situation is not accurately predictable, only the interaction between objects can be accurately know the final result.

Scenarios

Constantly changing needs of software, changes in the general demand is concentrated in a good place for the user layer, Internet applications, enterprise software, games, etc. are object-oriented programming to show their talents object-oriented programming is not all. For a software quality, the object-oriented programming is only used to solve scalability.

Function using object-oriented design

Functional OOD

#方案一
def dog(name,gender,type):
    # 狗的动作
    def jiao(dog):
        print('一条狗[%s],汪汪汪' % dog['name'])
    def chi_shi(dog):
        print('一条[%s] 正在吃屎' % dog['type'])
    #狗的特征
    dog1 = {
        'name':name,
        'gender': gender,
        'type': type,
        'jiao':jiao,
        'chi_shi':chi_shi,
    }
    return dog1
d1=dog('wj','母','中华田园犬')
d2=dog('qf','公','藏敖')
print(d1)
print(d2)
d1['jiao'](d1)
d2['chi_shi'](d2)

#方案二(改进)
def dog(name,gender,type):
    # 狗的动作
    def jiao(dog):
        print('一条狗[%s],汪汪汪' % dog['name'])
    def chi_shi(dog):
        print('一条[%s] 正在吃屎' % dog['type'])
    def init(name,gender,type):
        dog1 = {
            'name':name,
            'gender': gender,
            'type': type,
            'jiao':jiao,
            'chi_shi':chi_shi,
        }
        return dog1
    return init(name,gender,type)

d1=dog('wj','母','中华田园犬')
d2=dog('qf','公','藏敖')
print(d1)
print(d2)
d1['jiao'](d1)
d2['chi_shi'](d2)

Object-oriented design practice

def school(name,addr,type):
    def init(name, addr, type):
        sch = {
            'name': name,
            'addr': addr,
            'type': type,
            'kao_shi': kao_shi,
            'zhao_sheng': zhao_sheng,
        }
        return sch
    def kao_shi(school):
        print('%s 学校正在考试' %school['name'])
    def zhao_sheng(school):
        print('%s %s 正在招生' %(school['type'],school['name']))
    return  init(name,addr,type)

s1=school('文天','郑蒲岗','公立学校')
print(s1)
print(s1['name'])

s1['zhao_sheng'](s1)

s2=school('清华','北京','公立学校')

print(s2)
print(s2['name'],s2['addr'],s2['type'])
s2['zhao_sheng'](s2)

Object class (class using the object-oriented design)

The concept of classes and objects

Category is the category, type, object-oriented design is the most important concepts before we learn from the object is a combination of characteristics and skills, and the category is a series of similar objects and features a combination of skills.

So the question is, is to have a concrete existence of an object (such as the presence of a specific person)? It would first was the concept? This issue needs to look at two cases

In the real world: certainly the first, the object, and then have class

The world is certainly to be a variety of object actually exists, then with the development of human civilization, human beings different viewpoints summed up the different categories, such as the concept of human beings, animals, plants, etc., that is, said object is a concrete existence, while the class is only a concept, was not real, for example, you can not tell me what specifically refers to the human individual.

In the program: sure that define the class, after generating the object

This function is similar to using the: first-defined function, the function is called.
Class is the same: in the program need to define the class. After calling class.
Not the same: the calling function will execute the function body code and returns the result of execution of the function body. The class will have to call an object, the object is returned.

Define a class (the real world, the world program)

Follow the steps above, we define a class (we stand on the school's point of view, all of you are students)

In the real world: certainly the first, the object, and then have class

In the real world, standing day of school text angle: first, the object, and then have class

Real object

对象1:李易峰
    特征:
        学校=文天
        姓名=李易峰
        性别=男
        年龄=23
    技能:
        学习
        吃饭
        睡觉

对象2:勇虹王者
    特征:
        学校=文天
        姓名=勇虹王者
        性别=男
        年龄=28
    技能:
        学习
        吃饭
        睡觉

对象3:秋名山
    特征:
        学校=文天
        姓名=秋名山
        性别=男
        年龄=18
    技能:
        学习
        吃饭
        睡觉

The reality of class

现实中的学生类
    相似的特征:
        学校=文天
    相似的技能:
        学习
        吃饭
        睡觉

In the program: sure that define (s), the use of the class (for generating object)

In the Python program with the class关键字definition and in the program 特征with 变量identity, 技能with 函数identity, class and therefore the most common is simply: 变量and 函数definitions.

Program in class

class WentianStudent:
    school='文天'
    def learn(self):
        print('is learning')
        
    def eat(self):
        print('is eating')
    
    def sleep(self):
        print('is sleeping')

#注意
#1.类中可以有任意python代码,这些代码在类定义阶段便会执行,因而会产生新的名称空间,用来存放类的变量名与函数名,可以通过WentianStudent.__dict__查看
#2.类中定义的名字,都是类的属性,点是访问属性的语法
#3.对于经典类来说我们可以通过该字典操作类名称空间的名字(新式类有限制),但python为我们提供专门的语法

Objects in the program

Calling a class, or called instantiation, the object to obtain

s1=WentianStudent()
s2=WentianStudent()
s3=WentianStudent()

#如此,s1、s2、s3都一样了,而这三者除了相似的属性之外还各种不同的属性,这就用到了__init__

'''
__init__方法:
            __init__方法是在对象产生之后才会执行,只用来为对象进行初始化操作
            可以有任意代码,但一定不能有返回值(可以返回None,但是没有意义)
'''

class WentianStudent:
    ......
    def __init__(self,name,age,sex):
        self.name=name
        self.age=age
        self.sex=sex
    ......


#先调用类产生空对象s1,然后调用OldboyStudent.__init__(s1,'李易峰','男',23)
s1=WentianStudent('李易峰','男',23) 

s2=WentianStudent('勇虹王者','男',28)
s3=WentianStudent('秋名山','男',18)

Usage program classes and objects

Usage class program

.:专门用来访问属性,本质操作的就是__dict__
WentianStudent.school #等于经典类的操作WentianStudent.__dict__['school']
WentianStudent.school='文天' #等于经典类的操作WentianStudent.__dict__['school']='文天'
WentianStudent.x=1 #等于经典类的操作WentianStudent.__dict__['x']=1
del WentianStudent.x #等于经典类的操作WentianStudent.__dict__.pop('x')

Usage object program

#执行__init__,s1.name='秋名山',很明显也会产生对象的名称空间
s2.__dict__
{'name': '勇虹王者', 'age': '男', 'sex': 28}

s2.name #s2.__dict__['name']
s2.name='勇虹王者' #s2.__dict__['name']='勇虹王者'
s2.course='python' #s2.__dict__['course']='python'
del s2.course #s2.__dict__.pop('course')

A detailed explanation of the initialization method __init__

#方式一、为对象初始化自己独有的特征
class People:
    country='China'
    x=1
    def run(self):
        print('----->', self)

# 实例化出三个空对象
obj1=People()
obj2=People()
obj3=People()

# 为对象定制自己独有的特征
obj1.name='雯婧'
obj1.age=18
obj1.sex='female'

obj2.name='樵夫'
obj2.age=28
obj2.sex='male'

obj3.name='书呆子'
obj3.age=28
obj3.sex='male'

# print(obj1.__dict__)
# print(obj2.__dict__)
# print(obj3.__dict__)
# print(People.__dict__)



#方式二、为对象初始化自己独有的特征
class People:
    country='China'
    x=1
    def run(self):
        print('----->', self)

# 实例化出三个空对象
obj1=People()
obj2=People()
obj3=People()

# 为对象定制自己独有的特征
def chu_shi_hua(obj, x, y, z): #obj=obj1,x='雯婧',y=18,z='female'
    obj.name = x
    obj.age = y
    obj.sex = z

chu_shi_hua(obj1,'雯婧',18,'female')
chu_shi_hua(obj2,'樵夫',28,'male')
chu_shi_hua(obj3,'书呆子',28,'male')




#方式三、为对象初始化自己独有的特征
class People:
    country='China'
    x=1

    def chu_shi_hua(obj, x, y, z): #obj=obj1,x='雯婧',y=18,z='female'
        obj.name = x
        obj.age = y
        obj.sex = z

    def run(self):
        print('----->', self)


obj1=People()
# print(People.chu_shi_hua)
People.chu_shi_hua(obj1,'雯婧',18,'female')

obj2=People()
People.chu_shi_hua(obj2,'樵夫',28,'male')

obj3=People()
People.chu_shi_hua(obj3,'书呆子',28,'male')




# 方式四、为对象初始化自己独有的特征
class People:
    country='China'
    x=1

    def __init__(obj, x, y, z): #obj=obj1,x='雯婧',y=18,z='female'
        obj.name = x
        obj.age = y
        obj.sex = z

    def run(self):
        print('----->', self)

obj1=People('雯婧',18,'female') #People.__init__(obj1,'雯婧',18,'female')
obj2=People('樵夫',28,'male') #People.__init__(obj2,'樵夫',28,'male')
obj3=People('书呆子',28,'male') #People.__init__(obj3,'书呆子',28,'male')


# __init__方法
'''
强调:
   1、该方法内可以有任意的python代码
   2、一定不能有返回值
'''

class People:
    country='China'
    x=1

    def __init__(obj, name, age, sex): #obj=obj1,x='雯婧',y=18,z='female'
        # if type(name) is not str:
        #     raise TypeError('名字必须是字符串类型')
        obj.name = name
        obj.age = age
        obj.sex = sex


    def run(self):
        print('----->', self)


# obj1=People('雯婧',18,'female')
obj1=People(3537,18,'male')

# print(obj1.run)
# obj1.run() #People.run(obj1)
# print(People.run)

Class Notes

1. The stations at different angles, the definition of the class is different, the demand for detailed analysis of the real objects
2 in reality not exactly equal to the class-based program, such as a corporate reality, sometimes the program need to split into departments class, business class ......
3. sometimes in order to program requirements, the program may also define reality does not exist in the class, such as policy classes, in reality does not exist, but in the program it is a very common type

python class built special properties

类名.__name__# 类的名字(字符串)
类名.__doc__# 类的文档字符串
类名.__base__# 类的第一个父类(在讲继承时会讲)
类名.__bases__# 类所有父类构成的元组(在讲继承时会讲)
类名.__dict__# 类的字典属性
类名.__module__# 类定义所在的模块
类名.__class__# 实例对应的类(仅新式类中)

Guess you like

Origin www.cnblogs.com/chao-sir/p/12636051.html