Python 官方文档:入门教程 => 点击学习
Python 类的继承详解 python既然是面向对象的,当然支持类的继承,Python实现类的继承比javascript简单。 Parent类: class Parent: parentAt
Python 类的继承详解
python既然是面向对象的,当然支持类的继承,Python实现类的继承比javascript简单。
Parent类:
class Parent:
parentAttr = 100
def __init__(self):
print("parent Init")
def parentMethod(self):
print("parentMethod")
def setAttr(self,attr):
self.parentAttr = attr
def getAttr(self):
print("ParentAttr:",Parent.parentAttr)
Child类
class Child(Parent):
def __init__(self):
print("child init")
def childMethod(self):
print("childMethod")
调用
p1 = Parent();
p1.parentMethod();
c1 = Child();
c1.childMethod();
输出:
parent Init
parentMethod
child init
childMethod
Press any key to continue . . .
Python支持多继承
class A: # 定义类 A
.....
class B: # 定义类 B
.....
class C(A, B): # 继承类 A 和 B
.....
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
--结束END--
本文标题: Python 类的继承实例详解
本文链接: https://www.lsjlt.com/news/13536.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0