python staticmethod&classmethod
python中的这两种方法都通过修饰器来完成
静态方法:
不需要传递类对象或者类的实例
可以通过类的实例.方法名a().foo()或者类名.方法a.foo()名来访问
当子类继承父类时,且实例化子类时,这个实例其实时父类的,而非子类的
静态方法的调用和普通方法的调用一样,只不过前面加个类名。
静态方法并没有太大卵用,主要考虑代码的封装性,可维护性
类方法:
默认传递类对象本身cls,当然可以利用类中的方法
可以通过类的实例.方法名a().foo()或者类名.方法a.foo()名来访问
当子类继承父类时,方法正常使用
class Date(object):
day = 0
month = 0
year = 0
def __init__(self, day=0, month=0, year=0): #存储初始的信息
self.day = day
self.month = month
self.year = year
print self.day, self.month, self.year
@classmethod
def from_string(cls,date_string):
day, month, year = map(int, date_string.split('-'))
date1 = cls(day, month, year) #cls是个类对象,不是实例
return date1 #返回Date实例
def foo(self):
print 'I am foo' #普通的方法
@staticmethod
def is_date_valid(date_string): #验证方法封装在类中
day, month, year = map(int, date_string.split('-'))
return day <= 31 and month <= 12 and year <= 3999
---------------------------------------------------------------------------------------------
a=Date(1,2,3)
1 2 3
>>> a=Date.from_string('09-08-2015')
9 8 2015
>>> a.foo()
I am foo
>>> Date.is_date_valid('10-10-2015')
True
>>> type(Date.is_date_valid)
<type 'function'>
python staticmethod&classmethod的更多相关文章
- Python staticmethod classmethod 普通方法 类变量 实例变量 cls self 概念与区别
类变量 1.需要在一个类的各个对象间交互,即需要一个数据对象为整个类而非某个对象服务. 2.同时又力求不破坏类的封装性,即要求此成员隐藏在类的内部,对外不可见. 3.有独立的存储区,属于整个类. ...
- python staticmethod classmethod
http://www.cnblogs.com/chenzehe/archive/2010/09/01/1814639.html classmethod:类方法staticmethod:静态方法 在py ...
- python staticmethod,classmethod方法的使用和区别以及property装饰器的作用
class Kls(object): def __init__(self, data): self.data = data def printd(self): print(self.data) @st ...
- python @staticmethod @classmethod self cls方法区别
一直在用这些东西,但是又从来没有总结过,正好今日想起来就总结一下这些东西 @staticmethod 静态方法,名义上归属类管理,不能使用类变量和实例变量,类的工具包放在函数前,不能访问类属性和实例属 ...
- python staticmethod和classmethod(转载)
staticmethod, classmethod 分别被称为静态方法和类方法. staticmethod 基本上和一个全局函数差不多,只不过可以通过类或类的实例对象(python里只说对象总是容易产 ...
- python类方法@classmethod与@staticmethod
目录 python类方法@classmethod与@staticmethod 一.@classmethod 介绍 语法 举例 二.@staticmethod 介绍 语法 举例 python类方法@cl ...
- Python staticmethod() 函数
Python staticmethod() 函数 Python 内置函数 python staticmethod 返回函数的静态方法. 该方法不强制要求传递参数,如下声明一个静态方法: class ...
- 第7.16节 案例详解:Python中classmethod定义的类方法
第7.16节 案例详解:Python中classmethod定义的类方法 上节介绍了类方法定义的语法以及各种使用的场景,本节结合上节的知识具体举例说明相关内容. 一. 案例说明 本节定义的一个 ...
- 第7.15节 Python中classmethod定义的类方法详解
第7.15节 Python中classmethod定义的类方法详解 类中的方法,除了实例方法外,还有两种方法,分别是类方法和静态方法.本节介绍类方法的定义和使用. 一. 类方法的定义 在类中定 ...
随机推荐
- java学习方向及主要内容
Java分成J2ME(移动应用开发),J2SE(桌面应用开发),J2EE(Web企业级应用),所以java并不是单机版的,只是面向对象语言.建议如果学习java体系的话可以这样去学习: *第一阶段:J ...
- An O(ND) Difference Algorithm and Its Variations (1986)
http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.4.6927 The problems of finding a longest com ...
- redis07-----Redis持久化配置
Redis持久化配置 持久化: 即把数据存储于断电后不会丢失的设备中,通常是硬盘. 常见的持久化方式: 主从:通过从服务器保存和持久化,如mongoDB的replication sets配置. 淘宝是 ...
- stringBuffer、StringBuilder、排序、Arrays、Jdk1.5新特性(java基础知识十三)
1.StringBuffer类的概述 * A:StringBuffer类概述 * 通过JDK提供的API,查看StringBuffer类的说明 * 1.线程安全的可变字符序列. * 2.可将字符串缓冲 ...
- 多态、抽象类、接口、区别(java基础知识九)
1.多态的概述以及代码体现 * A:多态概述 * 事物存在的多种形态 * B:多态前提 * a:要有继承关系. * 一个类是父类,一个类是子类 * b:要有方法重写. * c:要有父类引用指向子类对象 ...
- CSS自定义文件上传按钮样式,兼容主流浏览器
解决办法:使用text文本框及a链接模拟文件上传按钮,并且把文件上传按钮放在他们上面,并且文件上传按钮显示透明.1.图片2. [代码][HTML]代码 <div class="b ...
- How to run Media SDK samples on Skylake【转载】
In the last few days, we have seen lot of concern for using Intel® Media 2016 on 6th generation Inte ...
- SPOJ:Dandiya Night and Violence(Bitset优化)
It is Dandiya Night! A certain way how dandiya is played is described: There are N pairs of people p ...
- [Selenium] Explicit wait 方法
(1) new WebDriverWait(driver, 10). until(ExpectedConditions.elementToBeClickable(locator)); (2) ne ...
- Ordered Fractions
链接 分析:遍历一下,求个gcd即可,最后按照ans排序并去重 /* PROB:frac1 ID:wanghan LANG:C++ */ #include "iostream" # ...