类和对象 Python开发之路

参考Python开发之路

1、不要简单的认为面向对象就是使用class A ,面向对象是一种编程思想,用函数也可以面向对象编程。

  面向过程、面向对象、函数式编程只是不同的编程范式,本身没有好坏之分,看用的人了。Linux的内核就是用C语言写成的,能说差吗?

2、用函数进行面向对象编程简单例子

def dog(name,gender,type):
    def run(dog):
        print('the {} run....'.format(dog['name']))

    def eat(dog):
        print('the {} eat....'.format(dog['name']))

    def init(name,gender,type):
        dog1 = {
            'name':name,
            'gender':gender,
            'type':type,
            'run':run,
            'eat':eat,
        }
        return dog1
    return init(name, gender, type)

d1 = dog('aa','','藏獒')
d1['run'](d1)

d2 = dog('bb','','藏獒')
d2['run'](d2)

猜你喜欢

转载自www.cnblogs.com/cc-world/p/12630995.html