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的更多相关文章

  1. caffe中使用python定义新的层

    转载链接:http://withwsf.github.io/2016/04/14/Caffe-with-Python-Layer/ Caffe通过Boost中的Boost.Python模块来支持使用P ...

  2. python定义函数时的默认返回值

    python定义函数时,一般都会有指定返回值,如果没有显式指定返回值,那么python就会默认返回值为None, 即隐式返回语句: return None 执行如下代码 def now(): prin ...

  3. python定义的一个简单的shell函数的代码

    把写代码过程中经常用到的一些代码段做个记录,如下代码段是关于python定义的一个简单的shell函数的代码. pipe = subprocess.Popen(cmd, stdout=subproce ...

  4. Python定义点击右上角关闭按钮事件

    Python定义点击右上角关闭按钮事件(Python defines the event of clicking the close button in the upper right corner) ...

  5. python定义接口继承类

    zxq547 python定义接口继承类invalid syntax解决办法 1 2 3 4 5 6 7 class s_all(metaclass=abc.ABCMeta):     #python ...

  6. python 定义函数

    在Python中,定义一个函数要使用def语句,依次写出函数名.括号.括号中的参数和冒号:,然后,在缩进块中编写函数体,函数的返回值用return语句返回. 我们以自定义一个求绝对值的my_abs函数 ...

  7. Python定义常量

    用Python实现常量 定义 # coding=utf-8 # const.py class ConstAssignError(Exception): pass class _const(object ...

  8. python定义类()中写object和不写的区别

    这里需要说明一下: python3中,类定义默认继承object,所以写不写没有区别 但在python2中,并不是这样 所以此内容是针对python2的,当然python3默认继承,不代表我们就傻乎乎 ...

  9. Python定义函数

    在Python中,定义一个函数要使用def语句,依次写出函数名.括号.括号中的参数和冒号:,然后,在缩进块中编写函数体,函数的返回值用return语句返回. 我们以自定义一个求绝对值的my_abs函数 ...

  10. python定义函数以及参数检查

    (转自廖雪峰网站) 函数定义 在Python中,定义一个函数要使用def语句,依次写出函数名.括号.括号中的参数和冒号:,然后,在缩进块中编写函数体,函数的返回值用return语句返回. 我们以自定义 ...

随机推荐

  1. freemarker常用标签解释三

    1 date,time,datetime 日期,时间,时间日期 <#assign test1 = "10/25/1995"?date("MM/dd/yyyy&quo ...

  2. datatables通过ajax调用渲染数据,怎么根据数据给td添加class

    html: <table id="table8" cellpadding="0" cellspacing="0" border=&qu ...

  3. 关于form组件的补充-------formChoice

    form组件的Choice字段 还是基于出版社和书的模型来详解 models.py(模型) from django.db import models # Create your models here ...

  4. 2.4 Rust Ownership

    What Is Ownership ownership这个单词有些不好翻译,刚开始就直接叫它“ownership”即可.这里简单说一下,我对它的理解, 从“数据结构与算法”的角度来看,ownershi ...

  5. java list分页

    用list分页时会遇到一个问题:用户先选好页码,再传条件查询的时候,会导致查询的那一页有可能什么都没有.这时候我们给他当前查询的最后一页的内容会比较友好. int pageSize; //用户查询的每 ...

  6. [转]jQuery TextBox Water Mark with asp.net

    本文转自:http://naspinski.net/post/jQuery-TextBox-Water-Mark-with-aspnet.aspx I stole majority of this c ...

  7. 封装RateLimiter 令牌桶算法

    自定义注解封装RateLimiter.实例: @RequestMapping("/myOrder") @ExtRateLimiter(value = 10.0, timeOut = ...

  8. Jetty数据同步使用

    1. Jetty简介 Jetty 是一个开源的servlet容器,它为基于Java的web容器,例如JSP和servlet提供运行环境.Jetty是使用Java语言编写的,它的API以一组JAR包的形 ...

  9. 深入学习hbase:表,列族,列标识,版本和cell

    HBase是面向列的分布式的数据库,和传统的关系型数据库有很大的不同:物理模型和逻辑模型.这里我们要首先讲一下HBase数据库相关的区别于关系型数据库的几个基本概念:          表:HBase ...

  10. 动态配置log4j2.xml日志输出文件的位置

    目标:根据启动jar时传进main()的参数动态修改日志位置 一.修改启动项 MainMapLookup.setMainArguments(args);注:不要在lookup设置之前初始化log(如: ...