iis服务器助手广告
返回顶部
首页 > 资讯 > 后端开发 > Python >python入门(坐标相加)
  • 330
分享到

python入门(坐标相加)

坐标入门python 2023-01-31 03:01:35 330人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

代码: 1 #coding=utf-8 2 class D2_point(object): 3 def __init__(self,x,y): 4 print '***ini

代码:

  1 #coding=utf-8
  2 class D2_point(object):
  3         def __init__(self,x,y):
  4                 print '***init'
  5                 self.x = x
  6                 self.y = y
  7 
  8         def __add__(self,oth):
  9                 print '***add'
 10                 return D2_point(self.x + oth.x,self.y + oth.y)
 11 
 12         def info(self):
 13                 print '***info'
 14                 print(self.x,self.y)
 15 
 16 class D3_point(D2_point):
 17         def __init__(self,x,y,z):
 18                 print '3Dinit'
 19                 super(D3_point,self).__init__(x,y)      #python2 中,super()需指定子类的继承
 20                 self.z = z
 21 
 22         def __add__(self,oth):
 23                 print '3Dadd'
 24                 return D3_point(self.x + oth.x,self.y + oth.y,self.z + oth.z)
 25 
 26         def info(self):
 27                 print '3Dinfo'
 28                 print(self.x,self.y,self.z)
 29 
 30 def myadd(a,b):         #注意此处是def,而不是class
 31         print '***myadd'
 32         return a + b
 33 
 34 if __name__ == '__main__':
 35         myadd(D2_point(1,2),D2_point(3,4)).info()
 36         #myadd(D3_point(1,2,3),D3_point(4,5,6)).info()

执行1:



执行2:



代码:

  1 #coding=utf-8
  2 class D2_point(object):
  3         def __init__(self,x,y):
  4                 print '***init'
  5                 self.x = x
  6                 self.y = y
  7 
  8         def __add__(self,oth):
  9                 print '***add'
 10                 return D2_point(self.x + oth.x,self.y + oth.y)
 11 
 12         def info(self):
 13                 print '***info'
 14                 print(self.x,self.y)
 15 
 16 class D3_point(D2_point):
 17         def __init__(self,x,y,z):
 18                 print '3Dinit'
 19                 #super(D3_point,self).__init__(x,y)     #Python2 中,super()需指定子类的继承
 20                 self.x = x
 21                 self.y = y
 22                 self.z = z
 23 
 24         def __add__(self,oth):
 25                 print '3Dadd'
 26                 return D3_point(self.x + oth.x,self.y + oth.y,self.z + oth.z)
 27 
 28         def info(self):
 29                 print '3Dinfo'
 30                 print(self.x,self.y,self.z)
 31 
 32 def myadd(a,b):         #注意此处是def,而不是class
 33         print '***myadd'
 34         return a + b
 35 
 36 if __name__ == '__main__':
 37         #myadd(D2_point(1,2),D2_point(3,4)).info()
 38         myadd(D3_point(1,2,3),D3_point(4,5,6)).info()

执行2+:




--结束END--

本文标题: python入门(坐标相加)

本文链接: https://www.lsjlt.com/news/187241.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作