python的继承多态以及异常处理

1.单继承

 1 # 动物类
 2 class Animal(object):
 3 
 4     def __init__(self, name):
 5         self. __name = name
 6 
 7     def run(self):
 8         print("%s 喜欢跑步" % self.__name)
 9 
10     def sleep(self):
11         print("%s 喜欢睡觉" % self.__name)
12 
13     def getName(self):
14         return self.__name
15 
16 # 猫类
17 class Cat(Animal):
18 
19     def __init__(self, name, type):
20         # 调用父类构造
21         # 采用super
22         super(Cat, self).__init__(name)
23         # 采用超类
24         # Cat.__init__(name)
25 
26         self.type = type
27 
28     def bark(self):
29         print("%s 喜欢叫" % self.getName())
30 
31 # 大黄猫
32 class BigYellowCat(Cat):
33 
34     def __init__(self, name):
35         # super()可以查找父类以及父类的父类的方法,但是构造访问上一个父类
36         # super(A,self),表示查找A类的父类以及父类的父类方法
37         super(Cat, self).__init__(name)
38         self.color = ""
39 
40     def __str__(self):
41         return ("%s 是一只大%s 猫" % (self.getName(), self.color))
42 
43 cat = BigYellowCat("TOM")
44 
45 print(cat)
46 cat.run()
47 cat.sleep()
48 cat.bark()

单继承,只需要在创建类是增加()添加继承的类名,即class A(B):....
上面可以看到,Cat类继承了Animal类,那么将获取Animal中所有公有的属性和方法,私有的属性和方法不继承
在创建对象时会默认调用构造方法,在继承中只会调用当前类的构造方法,有些属性定义在父类中,那么如何进行初始化呢?
这里采用两种方法,一种是采用超类,即类名.__init__(),但这里不推荐,这种格式一般是用来访问类属性的,
第二种方式是采用super(),通过super就能访问父类的所有方法,如果其父类有重写了父类的父类方法,那么会优先访问父类方法
当然需要定位访问某一个父类的方法,需要采用super(A, self),表示A类的父类(包括其父类的父类)

2.类方法和类属性

 1 # 动物类
 2 class Animal(object):
 3 
 4     # 定义类属性
 5     __count = 0
 6 
 7     # 定义类方法
 8     @classmethod
 9     def getCount(cls):
10         return cls.__count
11 
12     def __init__(self, name="TOM"):
13         self. name = name
14 
15     def __new__(cls, *args, **kwargs):
16         # 记录创建了多少个对象
17         cls.__count += 1
18         return super(Animal, cls).__new__(cls)
19 
20 
21 print(Animal.getCount())
22 
23 cat = Animal("")
24 dog = Animal("")
25 
26 print(Animal.getCount())

定义类属性比较简单,定义类方法需要关键字@classmethod,cls就表示当前类,与self的作用类似,调用时直接用类名.进行访问

3.静态方法

class Tool(object):

    @staticmethod
    def function():
        print("我是静态方法!")

Tool.function()

静态方法与类本身没有任何关系,Tool类可以将它理解为将多个函数聚集在一起的工具类,通常将能够全局用的方法封装在这里

4.多继承

 1 class Ear(object):
 2 
 3     def getEars(self):
 4 
 5         return " 耳朵"
 6 
 7     def fun(self):
 8         print("Ear")
 9 
10 class Nose(object):
11 
12     def getNose(self):
13 
14         return " 鼻子"
15 
16     def fun(self):
17         print("Nose")
18 
19 class Eyes(object):
20 
21     def getEyes(self):
22 
23         return " 眼睛"
24 
25     def fun(self):
26         print("Eyes")
27 
28 
29 class  Face (Nose, Eyes, Ear):
30 
31     def getFace(self):
32 
33         print("我有:", end="")
34 
35         # 能够调用所有继承的方法
36         print(self.getEars() + self.getEyes() + self.getNose())
37 
38 
39 
40 face = Face()
41 face.getFace()
42 face.fun()

多继承能够将所有父类的方法进行调用,但如果父类方法出现相同方法名时,根据继承的先后次序,优先运行最先加载的

5.多态

 1 class Person(object):
 2 
 3     def __init__(self, name):
 4         self.name = name
 5 
 6     def fun(self):
 7         print(self.name)
 8 
 9 class Student(Person):
10 
11     def __init__(self, name, score):
12         super(Student, self).__init__(name)
13         self.score = score
14 
15     def fun(self):
16         print("%s : %.2f分 " % (self.name, self.score))
17 
18 s1  = Person("ssss")
19 s1.fun()
20 
21 s2 = Student("xxxx", 80.2)
22 s2.fun()

python的多态比较简单,只是简单的涉及方法重写的问题,如需要深入了解可学习一下其他语言(如java)的多态