python基本学习-类的方法
来源:互联网 发布:淘宝商城女装韩版 编辑:程序博客网 时间:2024/06/05 14:18
类方法 @classmethod :绑定类
实例方法:绑定实例对象
静态方法 @staticmethod : 无绑定
特殊方法(魔法方法), init
class Date(object): def __init__(self,day,month,year): self.day = day self.month = month self.year = year def __str__(self): return "{0}-{1}-{2}".format(self.year,self.month,self.year) @classmethod def from_string(cls,date_as_string): year.month,day = map(int,date_as_string.split('-')) date1 = cls(day,month,year) return date1 #返回取决于调用的类cls @staticmethod def is_date_valid(date_as_string): year,month,day = map(int,date_as_string.split('-')) return day<=31 and month <=12 and year<=3999 @staticmethod def millenium(month,day): return Date(month,day,2000)class DateTime(Date): def __str__(self): return "{0}-{1}-{2}-00:00:00PM".format(self.year,self.month,self.year)
特殊方法
- 属性访问:getattr,setattr,
- 实例生成/类生成: init, new
- 数字计算: add, sub, mul,div,pow,round
- 调用方法: str,repr,len,bool
- 比较大小:cmp,lt,le,eq,ne,gt,ge
- 集合访问:setslice,getslice,getitem,setitem,contains
- 迭代器: iter,next
class Point(object): def __init__(self,x,y): self.x = x self.y = y def __sub__(self,other): assert isinstance(other,Point)#判断传入的数据是否为Point类 return Point(self.x+other.x,self.y+other.y) @property def xy(self): return (self.x,self.y) def __str__(self): # 显示print(a-b)结果 return "x={0},y={1}".format(self.x,self.y) def __repr__(self): #显示 a-b的解结果 return str(self.xy)# a = Point(50,60)# b = Point(30,40)# a - b
阅读全文
0 0
- python基本学习-类的方法
- Python基本学习-类的属性
- python 网络编程学习 http基本方法
- python类使用基本方法
- Python魔法方法--基本的魔法方法
- Python语言程序设计-学习笔记2:Python编程之基本方法
- python学习:扩展类的方法
- Python读取excel的基本方法-源代码
- python str.format()方法的基本用法
- Python解析JSON数据的基本方法
- python解析JSON数据的基本方法
- 【python学习.Mysql的基本操作使用】
- Python学习笔记(4)-基本的数据类型
- 学习了python的基本语法之后
- python学习-Day2-pycharm的基本使用
- Python--基本文件方法
- python常用基本方法
- python list 基本方法
- 关于Android studio报错gradle's dependency cache may be corrupt
- 遇到了“No USB devices or running emulators detected
- 10.6 负进制 2047
- 【安装nodejs】Redhat6.5下安装卸载nodejs
- #POJ3897#Maze Stretching(A* + 二分)
- python基本学习-类的方法
- NOIP2017提高A组模拟10.6】Biology
- 历届试题 核桃的数量
- Codeforces868B Race Against Time (模拟)
- spring中的context:include-filter和context:exclude-filter的区别
- java rmi 远程调用实现
- 洛谷P1064 金明的预算方案
- Python中的模块
- SCAUOJ——17235-Minecraft是个好游戏