python定义class
python也是面向对象的语言,类的重要性不言而喻。
class Animal:
def __init__(self,voice='hello'):
self.voice=voice
def __del__(self):
pass
def Say(self):
print self.voice
类名:Animal,构造函数__init__()有一个参数voice,默认值是'hello';我发现python里面的方法都有一个参数self,这样就可以很方便的用自己类中的属性、方法等。
__del__()是析构函数。
如何运行,打开python->import sys->sys.path.append("/py/"),根据自己路径->import animal,文件名->a=animal.Animal()创建该类的对象->a.Say(),调用该类的方法。
类的继承:
class Animal:
def __init__(self,voice='hello'):
self.voice=voice
def __del__(self):
pass
def Say(self):
print self.voice
def Run(self):
pass
class Cat(Animal):
def MyVoice(self,voice):
self.voice=voice
def Run(self):
print 'I am a cat.'
Cat类继承自Animal类,并新加了MyVoice()方法,重载了父类的Run()方法。
python定义class的更多相关文章
- caffe中使用python定义新的层
转载链接:http://withwsf.github.io/2016/04/14/Caffe-with-Python-Layer/ Caffe通过Boost中的Boost.Python模块来支持使用P ...
- python定义函数时的默认返回值
python定义函数时,一般都会有指定返回值,如果没有显式指定返回值,那么python就会默认返回值为None, 即隐式返回语句: return None 执行如下代码 def now(): prin ...
- python定义的一个简单的shell函数的代码
把写代码过程中经常用到的一些代码段做个记录,如下代码段是关于python定义的一个简单的shell函数的代码. pipe = subprocess.Popen(cmd, stdout=subproce ...
- Python定义点击右上角关闭按钮事件
Python定义点击右上角关闭按钮事件(Python defines the event of clicking the close button in the upper right corner) ...
- python定义接口继承类
zxq547 python定义接口继承类invalid syntax解决办法 1 2 3 4 5 6 7 class s_all(metaclass=abc.ABCMeta): #python ...
- python 定义函数
在Python中,定义一个函数要使用def语句,依次写出函数名.括号.括号中的参数和冒号:,然后,在缩进块中编写函数体,函数的返回值用return语句返回. 我们以自定义一个求绝对值的my_abs函数 ...
- Python定义常量
用Python实现常量 定义 # coding=utf-8 # const.py class ConstAssignError(Exception): pass class _const(object ...
- python定义类()中写object和不写的区别
这里需要说明一下: python3中,类定义默认继承object,所以写不写没有区别 但在python2中,并不是这样 所以此内容是针对python2的,当然python3默认继承,不代表我们就傻乎乎 ...
- Python定义函数
在Python中,定义一个函数要使用def语句,依次写出函数名.括号.括号中的参数和冒号:,然后,在缩进块中编写函数体,函数的返回值用return语句返回. 我们以自定义一个求绝对值的my_abs函数 ...
- python定义函数以及参数检查
(转自廖雪峰网站) 函数定义 在Python中,定义一个函数要使用def语句,依次写出函数名.括号.括号中的参数和冒号:,然后,在缩进块中编写函数体,函数的返回值用return语句返回. 我们以自定义 ...
随机推荐
- Swagger的坑
swagger.pathPatterns如果是譬如/w/.*,那么如果API中以w开头的描述就会在swagger-ui中显示不出来
- 斑马条码打印机通过js post 打印
<html lang="zh-ch"><head> <meta charset="utf-8"> <meta ht ...
- sql的几种常用锁简述
比较全的文章地址保存下:http://www.cnblogs.com/knowledgesea/p/3714417.html SELECT * FROM dbo.AASELECT * FROM dbo ...
- vmware 三种连接方式 如何设置独立ip
转载:https://blog.csdn.net/xiaoyangsavvy/article/details/73718473
- python 格式化时间含中文报错: 'locale' codec can't encode character '\u5e74'
执行下面代码报错: UnicodeEncodeError: 'locale' codec can't encode character '\u5e74' in position 2: Illegal ...
- 约瑟夫环问题poj1012
题意: 有k个坏人k个好人坐成一圈,前k个为好人(编号1~k),后k个为坏人(编号k+1~2k) 现在有一个报数m,从编号为1的人开始报数,报到m的人就要自动死去. 问当m为什么值时,可以使得在出现好 ...
- STL之 sort排序与检索(2019.1.18)
1.作用对象:数组 a[n] 头文件:#include<algorithm> 内容:sort(a,a+n) 功能:进行升序排序 内容:lower_bound(a,a+n,x) 功能:找到 ...
- 10-----BBS论坛
BBS论坛(十) 10.1.客户端权限验证功能完成 (1)cms/cms_profile 显示当前用户的角色和权限 <tr> <td>角色:</td> <td ...
- SpringBoot中通过实现WebMvcConfigurer完成参数校验
在Spring5.0和SpringBoot2.0中废弃了WebMvcConfigurerAdapter类. 现有两种解决方案 1 直接实现WebMvcConfigurer (官方推荐)2 直接继承We ...
- Fixed Function Shader
Fixed function shader(固定管线着色器) Shader "Custom/Text01" { //shader名称 ...