python 实例方法、静态方法、类方法
class Date:
#构造函数
def __init__(self, year, month, day):
self.year = year
self.month = month
self.day = day def tomorrow(self):
self.day += 1 @staticmethod
def parse_from_string(date_str):
year, month, day = tuple(date_str.split("-"))
return Date(int(year), int(month), int(day)) @staticmethod
def valid_str(date_str):
year, month, day = tuple(date_str.split("-"))
if int(year)>0 and (int(month) >0 and int(month)<=12) and (int(day) >0 and int(day)<=31):
return True
else:
return False @classmethod
def from_string(cls, date_str):
year, month, day = tuple(date_str.split("-"))
return cls(int(year), int(month), int(day)) # 这里如果类的名字改变了的话,我们返回类实例的时候,就不用返回类名称了 def __str__(self):
return "{year}/{month}/{day}".format(year=self.year, month=self.month, day=self.day) if __name__ == "__main__":
new_day = Date(2018, 12, 31)
new_day.tomorrow()
print(new_day) #2018-12-31
date_str = "2018-12-31"
year, month, day = tuple(date_str.split("-"))
new_day = Date(int(year), int(month), int(day))
print (new_day) #用staticmethod完成初始化
new_day = Date.parse_from_string(date_str)
print (new_day) #用classmethod完成初始化
new_day = Date.from_string(date_str)
print(new_day) print(Date.valid_str("2018-12-32"))
python 实例方法、静态方法、类方法的更多相关文章
- 【面试必问】python实例方法、类方法@classmethod、静态方法@staticmethod和属性方法@property区别
[面试必问]python实例方法.类方法@classmethod.静态方法@staticmethod和属性方法@property区别 1.#类方法@classmethod,只能访问类变量,不能访问实例 ...
- python 实例方法,类方法和静态方法
在学习python代码时,看到有的类的方法中第一参数是cls,有的是self,经过了解得知,python并没有对类中方法的第一个参数名字做限制,可以是self,也可以是cls,不过根据人们的惯用用法, ...
- Python 实例方法、类方法、静态方法的区别与作用
Python中至少有三种比较常见的方法类型,即实例方法,类方法.静态方法.它们是如何定义的呢?如何调用的呢?它们又有何区别和作用呢?且看下文. 首先,这三种方法都定义在类中.下面我先简单说一下怎么定义 ...
- python 实例方法,类方法,静态方法,普通函数
python中有实例方法,类方法,静态方法,普通函数 类方法需要@ classmethod 修饰并且有个隐藏参数 cls,实例方法必须有个参数 self, 静态方法必须有 @staticmethod修 ...
- python类的实例方法\静态方法\类方法区别解析(附代码)
前言 搞了好久python,一直搞得不太清楚这几种类的方法,今天花时间好好测试一下,算是弄懂点皮毛吧. 三种方法的不同 先剽窃个图看一下 可以看到,实例是三种方法都可以调用的,而类只可以调用两种.所以 ...
- python 实例方法,类方法,静态方法
实例方法 class Human(object): def __init__(self, weight): self.weight = weight def get_weight(self): ret ...
- python 实例方法、类方法和静态方法
#!/usr/bin/env python3.6 #-*- coding:utf-8 -*- # class Person(object): city = 'Beijing' def __init__ ...
- python类:类方法和静态方法
http://blog.csdn.net/pipisorry/article/details/49516185 面相对象程序设计中,类方法和静态方法是经常用到的两个术语.逻辑上讲:类方法是只能由类名调 ...
- python实例方法、静态方法和类方法
Python中至少有三种比较常见的方法类型,即实例方法,类方法.静态方法.它们是如何定义的呢?如何调用的呢?它们又有何区别和作用呢?且看下文. 首先,这三种方法都定义在类中.下面我先简单说一下怎么定义 ...
- python中的实例方法、类方法、静态方法的区别
Python 除了拥有实例方法外,还拥有静态方法和类方法,跟Java相比需要理解这个类方法的含义. class Foo(object): def test(self)://定义了实例方法 print( ...
随机推荐
- FTP安装及配置
在centos7安装ftp服务 yum install -y vsftpd 启动服务 systemctl start vsftpd 自启动 systemctl enable vsftpd 查看端口 注 ...
- 原子类解决i++问题
原子类解决i++问题 import java.util.concurrent.atomic.AtomicInteger; /** * 一个完整的i++,多线程并发安全问题演示.及使用java.util ...
- [译]Vulkan教程(19)渲染和呈现
[译]Vulkan教程(19)渲染和呈现 Rendering and presentation 渲染和呈现 Setup 设置 This is the chapter where everything ...
- Java的BIO和NIO很难懂?用代码实践给你看,再不懂我转行!
本文原题“从实践角度重新理解BIO和NIO”,原文由Object分享,为了更好的内容表现力,收录时有改动. 1.引言 这段时间自己在看一些Java中BIO和NIO之类的东西,也看了很多博客,发现各种关 ...
- numpy-np.ceil,np.floor,np.expand_dims方法
np.ceil(多维数组):对多维数组的各个数向上取整 np.floor(多维数组):对多维数组的各个数向下取整 np.expand_dims(x,axis = 0):在x的第一维度上插入一个维度,a ...
- Jupyter Notebook 使用小记
简介 Jupyter Notebook 是一款几乎综合所有编程语言,能够把软件代码.计算输出.解释文档.多媒体资源整合在一起的多功能科学计算平台.具有如下优点: 整合所有资源 交互性编程体验 零成本重 ...
- attempted to return null from a method with a primitive return type (int).
java接口文件 package com.cyb.ms.mapper; import org.apache.ibatis.annotations.Param; public interface Acc ...
- WinForms项目升级.Net Core 3.0之后,没有WinForm设计器?
目录 .NET Conf 2019 Window Forms 设计器 .NET Conf 2019 2019 9.23-9.25召开了 .NET Conf 2019 大会,大会宣布了 .Net Cor ...
- 资深架构师教你String 常量池、 String.itern()
什么是常量 用final修饰的成员变量表示常量,值一旦给定就无法改变! final修饰的变量有三种:静态变量.实例变量和局部变量,分别表示三种类型的常量. Class文件中的常量池 在Class文件结 ...
- python的exe反编译
目录 python的exe反编译 方法一.使用archive_viewer.py提取pyc 方法二.使用pyinstxtractor.py提取pyc python的exe反编译 驱动人生样本为pyth ...