python创建一个类很简单只需要定义它就可以了.
复制代码
1
2class Cat: pass
就像这样就可以了,通过创建子类我们可以继承他的父类(超类)的方法。这里重新写一下cat
复制代码
1
2
3
4
5
6
7
8class Cat: name = 'cat' class A(Cat): pass print A.name # cat
经典类
我们也可以这样,让A多继承。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12class Cat: name = 'cat' class Dog: name = 'dog' class A(Cat, Dog): pass print A.name # cat
如果Cat类没有name属性呢?
复制代码
1
2
3
4
5class Cat: pass ... print A.name # dog
A就会在其他的父类中找name这个属性。如果继承的两个父类都是继承自Animal类而Animal类也有name属性呢?
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16class Animal: name = 'animal' class Cat(Animal): pass class Dog(Animal): name = 'dog' class A(Cat, Dog): pass print A.name # animal
这样A就不会在Dog类中找而是会在Animal上找到name, 这种类叫经典类。类的解析顺序是一种从左到右深度优先的搜索。也就是A–> Cat–> Animal –> Dog。
新式类
python还有一种创建类的方式,就是使用新式类(建议使用), 都继承自object这个基类, 新式类的搜索规则是从左到右逐级查询。也就是A–> Cat –> Dog –> Animal。
复制代码
1
2class Cat(object): pass
最后
以上就是高挑紫菜最近收集整理的关于举例讲解Python面向对象编程中类的继承的全部内容,更多相关举例讲解Python面向对象编程中类内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复