python 继承和多态
A_Snail Lv3

python 继承和多态

什么是继承?

在生活中,大家都应该听过这个词,比如儿子继承他老子的财产,那么在python中也有继承,但是有不少人比较难理解继承这个东西。
在python,所有的类都默认继承object类,因此object类也被称为基类,其他的类称为派生类,在python中继承就是 子类可以继承父类中的所有的非私有方法

什么是私有方法?

是指在 Python 的面向对象开发过程中,对象的某些方法或者称为函数只想在对象的内部被使用,但不想在外部被访问到这些方法或函数。
即:私有方法是对象不愿意公开的方法或函数。
同理 私有属性也是类同
那么用一个简单的代码来理解一下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 1. 定一个一个公共性类 (
class Person():
# 定义__init__初始化方法
def __init__(self,name,age,adress):
self.name = name
self.age = age
self.adress = adress

def eat(self):
print('i can eat food!')

def speak(self):
print('i can speak!')

# 定义个性类
class Teacher():
def __init__(self,name,age,adress):
self.name = name
self.age = age
self.adress = adress

def eat(self):
print('i can eat food!')

def speak(self):
print('i can speak!')
def __write(self):
print('i can wirte')


# 定义个性类 Student 继承person类,发现也可以调用成功
class Student(Person):
pass


p = Person('snail',21,'贵州')
teacher = Teacher('tom',10,'贵州')
s1 = Student('xiaowu',87,'四川')
s1.eat()
s1.speak()

# 继承可以继承父类中的所有非私有方法, 也就是这里的`__write`方法将不会被继承 `__` 是python中私有方法的语法定义

那么执行后你会发现即使Student类中并没有定义eat()方法和speak()方法 但是也同样能够输出 这就是继承
继承的语很简单 直接在类后面的括号里面跟上需要继承的类即可。

例如:

1
2
3
4
5
6
7
8
class B(object):
pass # 这里自己写需要的方法

class A(B):
pass

a = A()
a.B中所有公共方法。

那么我们想在父类继承来的方法里面执行子类的特有的代码怎么办,
这里就引入了新的名词重写

那么什么是重写呢?

我的理解是,从父类继承过来,只有父类的方法和属性,那么我自己想要在父类的基础上面修改方法和属性该怎么办呢,那么重写就是用来解决这个问题的。 也就是继承后,可以进行修改继承来的方法, 如果有系统的属性和方法,进行重写, 也就是从父类继承来的方法会进行重新定义。
例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Animals(object):
def eat(self):
print('i can eat')

def call(self):
print('i can call phone ')

class Dog(Animals):
def eat(self):
print(' i like eat gutouts')


class Cat(Animals):
def eat(self):
print('i can 捉老鼠')


wangcai = Dog()
wangcai.eat()
wangcai.call()

miao = Cat()
miao.eat()
miao.call()

运行后便可以理解什么是重写了,也就是不同的类继承同一个父类后,进行方法的重写,之后执行的结果与父类和另外的子类的结果不同。
那么问题又来啦,既然我们对父类的方法进行重写了,那如果我们还需要调用父类的方法,那该怎么办呢,对此,python引入了super()方法

super():调用父类的方法或属性,完整写法: super().属性 或者super.方法 就可以完成调用了

调用例字:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Car(object):
def __init__(self, brand, model, color):
self.brand = brand
self.model = model
self.color = color

def run(self):
print('i can run')


class GasolineCar(Car):
def __init__(self, brand, model, color):
super().__init__(brand, model, color)

def run(self):
print('i can run with gas')


class ElectricCar(Car):
def __init__(self, brand, model, color):
super().__init__(brand, model, color)
# 代表电池电量。
self.battery = 70

def run(self):
print(f'i can run whith electric,remind{self.battery}')


bmw = GasolineCar('宝马', 'x5', '白色')
bmw.run()

tesla = ElectricCar('特斯拉', 'model3', '红色')
tesla.run()

这里用车来做例子,我们发现我们这里都是用的父类的方法对车进行初始化的。也就是我们在子类中并没有重新写初始化的方法。而是调用父类中的。

1.什么是多态

多态指的是一类实物有多种形态,
定义 多态是一种使用对象的方式,子类重写父类方法,调用不同子类对象的相同父类方法,可以产生不同的执行结果;

  1. 多态依赖继承
  2. 子类方法必须要重写父类方法。
    首先定义一个父类,其可能拥有多个子类对象,当我们调用一个公共方法时,传递的对象不同,则返回的结果不同。
    好处,调用灵活,有了多台,可以更容易编写出通用的代码,做出通用的编程,以适应需求的不断变化。、

不同对象访问相同方法,返回不同结果 就是多态

代码实现。
多态可以基于继承,也可以不基于继承。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class Fruit(object):
def makejuice(self):
print('i can make juice ')


class Apple(Fruit):
def makejuice(self):
print('i can make apple juice ')


class Banana(Fruit):
def makejuice(self):
print('i can make Banana juice ')


class Orange(Fruit):
def makejuice(self):
print('i can make Orange juice ')


# 利用多态。 定义一个service 公共方法接口
def service(obj):
obj.makejuice()


# apple = Apple()
# apple.makejuice() # 以往的方式,需要用一个调用一个
# banana = Banana()
# banana.makejuice() # 橘子也是 一样
# orange = Orange()
# orange.makejuice()

# 利用接口
apple = Apple()
banana = Banana()
orange = Orange()
for i in (apple,banana,orange):
service(i)

这里的service就是多态的体现。