0
点赞
收藏
分享

微信扫一扫

Python-类

梦为马 2022-03-26 阅读 116
python

类的形式

class Student:
	name#类属性 静态变量被所有类对象共享
	def __init__(self,age,city)::#构造函数 这里面的age和city就是每个类对象独有的参数
		self.name = 'tom'
	def fun(sef):#实例方法
		print('实例方法')
	@classmethod
	def eat(cls):#类方法
		print('%s在吃饭',self.name)
	@staticmethod
	def method():#这是一个静态方法
		print('方法')
stu = Student()
	

实例方法 静态方法 类方法的区别

实例方法

def fun(self):#实例方法
必须含有self 在调用时会自动把stu类对象最为参数传入

类方法

@classmethod
def fun(cls):#类方法
必须含有cls 在调用时会自动把Student类作为参数传入
cls.num_inst可以获取这个类有多少个对象

静态方法

@staticmethod
def fun():#静态方法
没有参数

举报

相关推荐

Python-类(实例化)

Python-元组

python-函数

python-集合

python-文件

Python-字典

python-报错

Python-继承

0 条评论