假设我有一个名为Car的类。我需要声明一个方法,以便可以打印汽车的名称
class.py
class Car:
# class variable
wheels = 4
# Instance attribute
def __init__(self,name):
self.name = name
newCar = Car("Civic")
print("my car is {}".format(newCar.name))
print("the number of its wheel is {}".format(newCar.wheels))
结果
my car is Civic
the number of its wheel is 4
所以在这里,为什么 self 被用作实例化类变量的参考?