镇场诗:
诚听如来语,顿舍世间名与利。愿做地藏徒,广演是经阎浮提。
愿尽吾所学,成就一良心博客。愿诸后来人,重现智慧清净体。
——————————————————————————————————————————
code:
class MyClass :
def __init__(self,long,weight):
self.long=long
self.weight=weight
#两个对象的长相加,宽不变.返回一个新的类
def __add__(self,others) :
return MyClass( self.long+others.long,self.weight+others.weight)
#两个对象的宽相减,长不变.返回一个新的类
def __sub__(self,others) :
return MyClass(self.long-others.long,self.weight-others.weight)
#说一下自己的参数
def intro(self):
print("长为",self.long," 宽为",self.weight)
a=MyClass(10,5)
a.intro()
b=MyClass(20,10)
b.intro()
c=b-a
c.intro()
d=a+b
d.intro()
result:
============= RESTART: C:\Users\Administrator\Desktop\myCode.py =============
长为 10 宽为 5
长为 20 宽为 10
长为 10 宽为 5
长为 30 宽为 15
>>>
——————————————————————————————————————————
博文的精髓,在技术部分,更在镇场一诗。Python版本3.5,系统 Windows7。
Python是优秀的语言,值得努力学习。我是跟着小甲鱼视频教程学习的,推荐。
我是一个新手,所以如果博文的内容有可以改进的地方,甚至有错误的地方,请留下评论,我一定努力改正,争取成就一个良心博客。
注:此文仅作为科研学习,如果我无意中侵犯了您的权益,请务必及时告知,我会做出改正。