1 Star 0 Fork 0

FelixBin/PythonLearn

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
类方法.py 493 Bytes
一键复制 编辑 原始数据 按行查看 历史
# _*_ coding:utf-8 _*_
# __author__ = 'WFaceBoss'
# __date__ = '2018-08-09 10:19'
class Dog(object):
name = "我是类变量"
def __init__(self, name):
self.name = name
@classmethod
def eat(self):
print("%s is eating food" % self.name)
d = Dog("TD")
d.eat() # 结果为:我是类变量 is eating food
'''
类方法通过@classmethod装饰器实现,类方法和普通方法的区别是, 类方法只能访问类变量,不能访问实例变量
'''
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/FelixBinCloud/PythonLearn.git
git@gitee.com:FelixBinCloud/PythonLearn.git
FelixBinCloud
PythonLearn
PythonLearn
master

搜索帮助