__init__构造方法

属性是变量,方法是函数
函数使用变量就是面向对象

self.是为了使变量能在类中跨函数使用。
不写self.   python只能在当前函数中找变量。

写上self.python会在当前类中找变量
class Mystuff(object):
	def __init__(self):
		self.age = 18
	
	def print_age(self):
		age=1
		print(age)
		print(self.age)
aa = Mystuff()
aa.print_age()

猜你喜欢

转载自blog.csdn.net/weixin_42540340/article/details/108347686