Python之路,第二十篇:Python入门与基础20

python3  面向对象4

supper 函数

supper(type, obj) 返回绑定超类的实例(要求obj必须为type类型的实例)

supper()   返回绑定的超类的实例,等同于(class, 实例方法的第一个参数),此方法必须用在方法内部;

作用:返回绑定超类的实例,用超类的实例来调用其自身的方法;

 1 class A(object):
 2     def hello(self):
 3         print("A类的hello(self)")
 4 
 5 
 6 class B(A):
 7     def hello(self):
 8         print("B类的hello(self)")
 9         #self.__class__.__base__.hello(self)  #A类的hello(self)
10 
11 
12 b = B()
13 b.hello()   #调用B类的方法
14 #调用基类的方法
15 super(B, b).hello() # A类的hello被调用,等同于B.__base__.hello(b)  #A类的hello(self)
View Code
 1 class A(object):
 2     def hello(self):
 3         print("A类的hello(self)")
 4 
 5 
 6 class B(A):
 7     def hello(self):
 8         print("B类的hello(self)")
 9         #self.__class__.__base__.hello(self)  #A类的hello(self)
10 
11     def super_hello(self): #此方法用来调用基类的hello方法
12         #self.hello() #调用B类的hello方法
13         super(B, self).hello() #可以简写为super().hello()
14 
15 b = B()
16 b.hello()   #调用B类的方法
17 #调用基类的方法
18 #super(B, b).hello() # A类的hello被调用,等同于B.__base__.hello(b)  #A类的hello(self)
19 b.super_hello() #A类的hello(self)
View Code

 

 

用于类的函数:

issubclass(cls,  类  或  类元组)

判断一个类是否是继承自其它的类,如果此类cls是类(class)或类元组中的一个派生子类,则返回True, 否则返回False;

查看帮助:

>>>help(__builtins__)可以查看所有内建类的帮助;

 1 class A:
 2     pass
 3 
 4 class B(A):
 5     pass
 6 
 7 class C(B):
 8     pass
 9 
10 class D(C):
11     pass
12 
13 #判断是否继承其他类
14 print(issubclass(C, A))  #True
15 print(issubclass(C, B))  #True
16 print(issubclass(A, C))  #False
17 print(issubclass(C, D))  #False
18 print(issubclass(C, (A, B, D)))  #True
19 #
20 print(issubclass(int, object))   #True
21 print(issubclass(int, bool))    #False
22 print(issubclass(bool, int))    #True
View Code

 

 

显示调用基类的构造方法:

def   __init__(self,  ....):

         .........

 1 #如何显示调用构造方法
 2 #class A(object):
 3 #    """docstring for A"""
 4 #    def __init__(self, arg):
 5 #        super(A, self).__init__()
 6 #        self.arg = arg
 7 
 8 
 9 class Human:
10     def __init__(self, name, age):
11         self.name = name
12         self.age = age
13 
14     def infos(self):
15         print("姓名:",self.name,"年龄:",self.age)
16 
17 
18 class Student(Human):
19     def __init__(self, name, age, score):
20         #self.name = name
21         #self.age = age
22         super(Student, self).__init__(name, age)# 可以简写为 super().__init__(name,age)
23         self.score = score
24 
25     def infos(self):
26         print("姓名:",self.name,
27               "年龄:",self.age,
28               "分数:",self.score)
29 
30 
31 h1 = Human("张三", 20)
32 h1.infos() #姓名: 张三 年龄: 20
33 
34 s1 = Student("李四",21, 100)
35 s1.infos() #姓名: 李四 年龄: 21 分数: 100
View Code

 

 

多态 polymorphic

什么是多态:

字面意思:“多种状态”;

多态是批在有继承/派生关系的类中,调用基类对象方法,实际能调用子类的覆盖方法现象叫多态;

多态调用的方法与对象相关,不与类型相关;

 1 class Shape: #图形类
 2     def drw(self):
 3         self.drawSelf()
 4 
 5 class Point(Shape):
 6     def drawSelf(self):
 7         print("正在画一个点.")
 8 
 9 class Circle(Point):
10     def drawSelf(self):
11         print("正在画一个圆.")
12 
13 shape = Point()
14 shape.drw() #正在画一个点.
15 
16 shape = Circle()
17 shape.drw() #正在画一个圆.
View Code

 

 

面向对象思想的特征:

1, 封装

2, 继承(派生)

3,多态

 1 class OrderSet(list):
 2     def add(self, n):
 3         for i in range(len(self)):
 4             self[i] += n
 5 
 6 
 7 L = OrderSet()
 8 L.append(10)
 9 L.append(19)
10 print(L) #[10, 19]
11 L.add(2)
12 print(L) #[12, 21]
View Code

 

封装

作用:封装是指隐藏类的实现细节,让使用者不关心这些细节;

注:python的封装是假的(模拟的)封装;

 

私有实例变量和方法:

python中,以双下划线开头"__"开头,不以双下划线结尾的标识符,为私有成员;

私有成员分为:私有属性和私有方法

私有成员在子类和类外部无法访问;

 1 class A:
 2     def __init__(self, args):
 3         self.__p = None
 4         self.__p = args
 5 
 6     def __private_method(self):
 7         print("你好,我是私有方法.")
 8 
 9     def showA(self):
10         print("self.__p",self.__p)
11         self.__private_method()
12 
13 class B(A):
14     def __init__(self):
15         super().__init__(0)
16 
17     def myfun(self):
18         pass
19         #print("self.__p", self.__p) #错的,不能使用私有属性
20         #self.__private_method()     #错的,不能使用私有方法
21 
22 
23 b = B()
24 b.myfun() #
25 
26 a = A(100)
27 a.showA()
28 #私有方法不能在类外部调用
29 #a.__private_method()
30 #私有属性不能在外部使用
31 #a.__p -= 2
32 #print(a.__p)
View Code

 

 

多继承 multiple  inheritance

多继承是指一个子类继承自两个或两个以上的基类;

多继承的语法:

class    类名(超类1,超类2,......):

           ......

 1 class Car: #汽车类
 2     def run(self, speed):
 3         print("汽车以",speed, "km/h的速度行驶")
 4 
 5 class Plane: #飞机类
 6     def fly(self, height):
 7         print("在海拔", height, "米高度飞行.")
 8 
 9 class PlaneCar(Car, Plane):
10     """PlaneCar 类 同时继承自汽车和飞机"""
11 
12 
13 pl = PlaneCar()
14 pl.fly(10000)
15 pl.run(250)
View Code

 

多继承的问题(缺陷): 标识符的冲突问题,要谨慎使用多继承;

 1 class A:
 2     def __init__(self):
 3         self.name = "A"
 4 
 5 class B:
 6     def __init__(self):
 7         self.name = "B"
 8 
 9 class AB(A, B):  #感觉A类、B类都可以用
10     def infos(self):
11         print(self.name) #
12 
13 
14 ab = AB()
15 ab.infos() #结果是"A" ; 不是"A","B".
16 #######################################
17 class A:
18     def __init__(self):
19         self.name = "A"
20 
21 class B:
22     def __init__(self):
23         self.name = "B"
24 
25 class AB(B, A):  #感觉A类、B类都可以用
26     def infos(self):
27         print(self.name) #
28 
29 
30 ab = AB()
31 ab.infos() #结果是"B" ; 不是"A","B".
View Code

 

PEP8  编码标准:

代码编写:

1,使用4个空格进行退进,不使用TAB,更不允许使用TAB和空格混用;

2,每行最大长度79个字节,超过部分使用反斜杠折行;

3,类和全局函数定义间隔两个空行,类内方法定义间隔一个空行,其他地方不加空行;

文档编排:

1,import部分 按标准,三方和自己编写的顺序依次排序,之间空一行;

2,不要在一名import 中导入多个库模块,如 import  os , sys, time应写成多行;

3, 尽可能用import  XX ,而不用from xx import yy 引用库, 因为可能出现名字冲突;

空格的使用:

1,各种右括号前不用加空格

     L = [1,2,3]  # OK

     L = { 1,2,3  }  #   } 前不要加空格;

2,逗号,冒号,分号前不要加空格;

3,函数左括号前不要加空格;

4,操作符左右各加一个空格,不要为了对齐增加空格;

5,函数的默认参数使用的赋值符左右省略空格(def  xxx(a=1,b=2):));

6,不要将多句语句写在同一行,尽量不要加用分号;。

7,if/for/while语句中,即使执行语句只有一句,也必须另起一行;

 

 

======== 

总结:

1,python中闭包必须满足三大特点 :1.闭包函数必须有内嵌函数   2.内嵌函数需要引用该嵌套函数上一级namespace中的变量  3.闭包函数必须返回内嵌函数;

2,如果你不想在异常发生时结束你的程序,只需在try里捕获它。

3,_foo: 以单下划线开头的表示的是 protected 类型的变量,即保护类型只能允许其本身与子类进行访问,不能用于 from module import *

========

 

posted on 2018-06-01 23:08  微子天明  阅读(303)  评论(0编辑  收藏  举报

导航