Python之道–实例和类的继承以及运算符重载
2013-06-14 20:09
603 查看
Python版本:2.7.4主机环境:Gentoo with Linux Kernel 3.4.36原创作品,转载请标明:http://blog.geekcome.com/archives/79Python中关于类和实例的关系比JAVA,C++等语言中的类之间的关系要简单的多。对于学过C++或JAVA语言的我们,类和对象已经很熟悉了,他们的设计思想似乎也已经融入了我们的身体,这里不再多说他们的概念。这里只是展示几个Python的类的实例,用来说明Python中的类和实例以及类的继承。下面的实例有三个类:FirstClass,SecondClass,ThordClassclass FirstClass:
def setdata(self,value):
self.data = value
def __init__(self,value):
self.data = value
def display(self):
print self.data
class SecondClass(FirstClass):
def display(self):
print ‘Current value is %s’ % self.data
class ThirdClass(SecondClass):
def __add__(self,other):
return ThirdClass(self.data + other)
def __str__(self):
return ‘ThirdClass: %s’ % self.data
def mul(self,value):
self.data *= value三个类的定义如上面所示,FirstClass是基类,SecondClass继承了FirstClass,而ThirdClass又继承了SecondClass。a = FirstClass(“First Class”)
a.display()
a.setdata(‘Again First Class’)
a.display()b = SecondClass(‘Second Class’)
b.display()
b.setdata(‘Again Second Class’)
b.display()c = ThirdClass(“Third Class”)
c.display()
c = c + ‘ Again’
c.display()
c.mul(8)
print c第三个类中通过定义__**__函数来重载了加法运算,重载了print打印字符串的内置函数str() 。函数的运行结果如下:First Class
Again First Class
Current value is Second Class
Current value is Again Second Class
Current value is Third Class
Current value is Third Class Again
ThirdClass: Third Class AgainThird Class AgainThird Class AgainThird Class AgainThird Class AgainThird Class AgainThird Class AgainThird Class Again
def setdata(self,value):
self.data = value
def __init__(self,value):
self.data = value
def display(self):
print self.data
class SecondClass(FirstClass):
def display(self):
print ‘Current value is %s’ % self.data
class ThirdClass(SecondClass):
def __add__(self,other):
return ThirdClass(self.data + other)
def __str__(self):
return ‘ThirdClass: %s’ % self.data
def mul(self,value):
self.data *= value三个类的定义如上面所示,FirstClass是基类,SecondClass继承了FirstClass,而ThirdClass又继承了SecondClass。a = FirstClass(“First Class”)
a.display()
a.setdata(‘Again First Class’)
a.display()b = SecondClass(‘Second Class’)
b.display()
b.setdata(‘Again Second Class’)
b.display()c = ThirdClass(“Third Class”)
c.display()
c = c + ‘ Again’
c.display()
c.mul(8)
print c第三个类中通过定义__**__函数来重载了加法运算,重载了print打印字符串的内置函数str() 。函数的运行结果如下:First Class
Again First Class
Current value is Second Class
Current value is Again Second Class
Current value is Third Class
Current value is Third Class Again
ThirdClass: Third Class AgainThird Class AgainThird Class AgainThird Class AgainThird Class AgainThird Class AgainThird Class AgainThird Class Again
相关文章推荐
- python运算符重载实例
- python类继承用法实例分析
- python实例pyspark以及python中文显示
- 判断一个类或实例是否继承自某类的python代码
- python基于mysql实现的简单队列以及跨进程锁实例详解
- Python面向对象之继承和组合用法实例分析
- Python面向对象类继承和组合实例分析
- python类继承,方法覆写,运算符重载
- Appium+XCUITest基于Python的操作实例以及环境搭建
- 回调函数的意义以及python实现实例
- pcapng文件的python解析实例以及抓包补遗
- 继承、多态性、重载的原理以及实例
- python多重继承实例
- Python中类型关系和继承关系实例详解
- Python学习27:继承以及多重继承方式,MRO(方法解释顺序)
- Python中的单继承与多继承实例分析
- Python2.7 以及 Python 3.5的实例方法,类方法,静态方法之间的区别及调用关系
- Python 类的继承实例详解
- python数字类型介绍以及创建数字值对象引用、删除数字值引用操作实例
- python keras (一个超好用的神经网络框架)的使用以及实例