调用方法有两种形式

实例调用

直接调用后序参数即可

类调用

调用时需要先加上实例

示例

class test1:
def pt(self,txt): #定义函数
test1.txt=txt
print(self.txt)
x=test1() #定义实例
x.pt('test') #实例调用
test1.pt(x,'test1') #类调用

定制被继承的方法

首先需要说明默认继承是你如果不去重载那么就是父类的,如果重载那么全部重来

比如继承构造如果不重载我们继承的就是父类的构造方法

示例

class test2:
def __init__(self,x,y):
test2.x=x
test2.y=y
def __str__(self):
return 'x=%d y=%d' %(test2.x,test2.y)
x1=test2(1,2)
class test3(test2):
def __str__(self):
return 'y=%d x=%d' %(test3.y,test3.x)
x2=test3(1,2)
print(x1)
print(x2)

但是我们如果想子类扩充一些父类的构造的话,按照原来的套路我们需要把父类的再写一遍而不是直接补充

这里我们可以定制被继承的方法

说的这么高大上实际上就是把被继承的方法复读一遍。。。。

示例

class test2:
def __init__(self,x,y):
test2.x=x
test2.y=y
def __str__(self):
return 'x=%d y=%d' %(test2.x,test2.y)
x1=test2(1,2)
class test3(test2):
def __init__(self,x,y,z):
test2.__init__(self,x,y)
test3.z=z
def __str__(self):
return 'y=%d x=%d z=%d' %(test3.y,test3.x,test3.z)
x2=test3(1,2,3)
print(x1)
print(x2)

这也可以用在别的地方

示例

class test2:
def __init__(self,x,y):
test2.x=x
test2.y=y
def pt(self):
print('x=%d y=%d' %(test2.x,test2.y))
x1=test2(1,2)
class test3(test2):
def __init__(self,x,y,z):
test2.__init__(self,x,y)
test3.z=z
def pt(self):
test2.pt(self)
print( 'z=%d' %(test3.z))
test2.pt(self)
x2=test3(1,2,3)
x1.pt()
x2.pt()

输出(省略x,y,z)

1 2

1 2

3

1 2

这种方法好像对于运算符重载来说么得用,好像因为运算符直接return?(存疑)

接口定义(目前太菜完全不理解为啥这么做)

在父类中定义一个预计要在子类中出现得对象函数

示例

class test4:
def pt(self):
self.pt1()
class test5(test4):
def pt1(self):
print('这是一个接口?')
x3=test5()
x3.pt()

首先在父类中定义了一个子类得pt接口输出用子类得pt1()

然后定义有pt1的子类

然后定义子类的对象

直接调用这个接口

最新文章

  1. Cesium原理篇:6 Render模块(4: FBO)
  2. Nginx添加到windows服务
  3. 【NodeJS线程】Boss和他的职员们
  4. How to backup your blogs on cnblogs
  5. pycharm5新版注册
  6. 【Python排序搜索基本算法】之深度优先搜索、广度优先搜索、拓扑排序、强联通&Kosaraju算法
  7. Lintcode: Flip Bits
  8. FB接口之 js调用支付窗口
  9. postgresql大批量数据导入方法
  10. PostgreSQL中的AnyArray例子
  11. highcharts 柱状图
  12. 【剑指offer】调整数组顺序
  13. VS找不到MFC90d.dll错误
  14. mac下CSV文件用FileReader、FileWriter读写乱码
  15. JWebFileTrans(JDownload): 一款可以从网络上下载文件的小程序(三),多线程断点下载
  16. Java 1.7 NQuery
  17. 寒假训练 A - A Knight's Journey 搜索
  18. Codeforces Global Round 1 - D. Jongmah(动态规划)
  19. maven 跳过test
  20. c# 中foreach 循环

热门文章

  1. git ignore的一些技巧
  2. Silverlight之控件应用总结(二)(4)
  3. A bad vacation
  4. vue商品详情页添加动画(eg)
  5. P2252 取石子游戏
  6. video 能播放声音不能播放视频,黑屏
  7. 使用JS分页 <span> beta 2.0 未封装的分页
  8. python获取主机名和用户名
  9. kafka启动时出现FATAL Fatal error during KafkaServer startup. Prepare to shutdown (kafka.server.KafkaServer) java.io.IOException: Permission denied错误解决办法(图文详解)
  10. A8ERP权限管理系统