python基础和注意点概要1

1. super
1) python 2.7中的例子:

class Animal(object):
    def __init__(self,name):
        self.name = name
    def greet(self):
        print "I am %s"%self.name

class Dog(Animal):  
    def greet(self):
        super(Dog,self).greet()        #Python3可以使用直接使用super().greet()
        print "wangwang"

dog = Dog("hh")
dog.greet()

2) 下面代码会输出什么

class A(object):
    def go(self):
        print "go A go!"
    def stop(self):
        print "stop A stop!"
    def pause(self):
        raise Exception("Not Implemented")

class B(A):
    def go(self):
        super(B, self).go()
        print "go B go!"

class C(A):
    def go(self):
        super(C, self).go()      #C 调用 A
        print "go C go!"
    def stop(self):
        super(C, self).stop()
        print "stop C stop!"

class D(B,C):
    def go(self):
        super(D, self).go()      #D > B > C 最终调用C
        print "go D go!"
    def stop(self):
        super(D, self).stop()
        print "stop D stop!"
    def pause(self):
        print "wait D wait!"
d = D()
d.go()

参考:

https://www.cnblogs.com/zhangqigao/p/6397853.html 问题10

 

以下参考:http://blog.csdn.net/jim_cainiaoxiaolang/article/details/51803625

3. 两个字典如何合并

a = {'a':1}
b = {'b':2}
#方法一:
print dict(a.items() + b.items())

#方法二:
c = a.copy()
c.update(b)
print c

#方法三
print dict(a, **b)

# **b解析:
# In Python, any function can accept multiple arguments with *;
# or multiple keyword arguments with **.

dict(a, **b) 即
dict({'a':1}, 'b'=2))

 

4. 面向对象的三大特征

1)封装

第一个层面:类和对象

第二个层面:类中把某些属性和方法定义为私用的

2)继承   注意多重继承的顺序

3)多态   

 

posted @ 2018-03-01 16:26  Hsinwang  阅读(145)  评论(0编辑  收藏  举报