十、对面向对象新的理解

看了一个视频,颠覆了之前对面向对象设计的理解,编程真的的很神奇,他的博客:

http://www.cnblogs.com/linhaifeng/p/7278389.html

之前以为面向对象就是class A(){},现在知道了只用函数也可以面向对象编程。

面向过程、面向对象、函数式编程只是不同的编程范式吧,本身没有好坏之分,看用的人了。

# -*- coding:utf-8 -*-
dog1={
    'name':'aa',
    'gender':'公',
    'type':'藏獒'
}
dog2={
    'name':'bb',
    'gender':'公',
    'type':'藏獒'
}

def eat(dog):
    print('the %s is eating'%dog['name'])
def jiao(dog):
    print('the %s is jiao'%dog['type'])

eat(dog1)
eat(dog2)
jiao(dog1)
jiao(dog2)
##########################################
dog1={
    'name':'aa',
    'gender':'公',
    'type':'藏獒'
}
dog2={
    'name':'bb',
    'gender':'公',
    'type':'藏獒'
}
person1={
    'name':'cc',
    'gender':'女',
    'type':'人'
}

def eat(dog):
    print('the %s is eating'%dog['name'])
def jiao(dog):
    print('the %s is jiao'%dog['type'])


eat(dog1)
eat(dog2)
jiao(dog1)
jiao(dog2)

eat(person1)
jiao(person1)#这时候人也可以调用狗的方法,显然不合适,应该把狗的方法和狗绑定到一块,想到了函数的封装
########################################################
def dog():
    def eat(dog):
        print('the %s is eating'%dog['name'])
    def jiao(dog):
        print('the %s is jiao'%dog['type'])

    dog1 = {
        'name': 'aa',
        'gender': '公',
        'type': '藏獒',
        'eat': eat,#函数名
        'jiao': jiao
    }
    return dog1

d1=dog()
print(d1)#{'gender': '公', 'jiao': <function dog.<locals>.jiao at 0x000000AB5F69CA60>, 'type': '藏獒', 'name': 'aa', 'eat': <function dog.<locals>.eat at 0x000000AB5F69C9D8>}
d1['jiao'](d1)#the 藏獒 is jiao,函数要有参数,这个参数就是他本身d1
########################################################################
######现在的问题是只有一条狗,这条狗d1在函数里已经写死了,如果有另外的狗还要重复代码,不能写死就从外面传进来################
def dog(name,gender,type):
    def eat(dog):
        print('the %s is eating'%dog['name'])
    def jiao(dog):
        print('the %s is jiao'%dog['type'])

    dog1 = {
        'name': name,
        'gender': gender,
        'type': type,
        'eat': eat,#函数名
        'jiao': jiao
    }
    return dog1

d1=dog('dd','公','京巴')#产生新的狗
d2=dog('ee','母','京巴2')#产生新的狗

print(d1)#{'gender': '公', 'jiao': <function dog.<locals>.jiao at 0x000000AB5F69CA60>, 'type': '藏獒', 'name': 'aa', 'eat': <function dog.<locals>.eat at 0x000000AB5F69C9D8>}
d1['jiao'](d1)#the 藏獒 is jiao,函数要有参数,这个参数就是他本身d1
d2['jiao'](d2)
######################################################
#再做一个小小的改进,加了一个Init函数用来初始化,这样dog()函数的结构很清晰,包含了三个功能明确的函数
def dog(name,gender,type):
    def init(name, gender, type):  # 用来初始化
        dog1 = {
            'name': name,
            'gender': gender,
            'type': type,
            'eat': eat,  # 函数名,,这时候放前面也不会报错,因为没有执行
            'jiao': jiao
        }
        return dog1
    def eat(dog):
        print('the %s is eating'%dog['name'])
    def jiao(dog):
        print('the %s is jiao'%dog['type'])
    return init(name,gender,type)



d1=dog('dd','公','京巴')
d2=dog('ee','母','京巴2')

print(d1)#{'gender': '公', 'jiao': <function dog.<locals>.jiao at 0x000000AB5F69CA60>, 'type': '藏獒', 'name': 'aa', 'eat': <function dog.<locals>.eat at 0x000000AB5F69C9D8>}
d1['jiao'](d1)#the 藏獒 is jiao,函数要有参数,这个参数就是他本身d1
d2['jiao'](d2)

猜你喜欢

转载自blog.csdn.net/cc27721/article/details/82656171