我试图弄清楚Python中的封装.我在shell中进行了一个简单的小测试,看看它是如何工作的,它不像我期望的那样工作.我无法让它发挥作用.这是我的代码:
class Car:
def __init__(self, carMake, yrMod):
self.__make = carMake
self.__yearModel = yrMod
self.__speed = 0
#Mutator Methods
def set_make(self, make):
self.__make = carMake
def set_model(self, yrMod):
self.__yearModel = yrMod
#def set_speed(self, speed):
#self.__speed = speed
#Accessor Methods
def get_make(self):
return self.__make
def get_yearModel(self):
return self.__yearModel
def get_speed(self):
return self.__speed
myCar=Car('Ford', 1968)
myCar2=Car('Nissan', 2012)
myCar.get_make()
'Ford'
myCar.set_make=('Porche')
myCar.get_make()
'Ford'
Run Code Online (Sandbox Code Playgroud)
为什么myCar.set_make没有将福特变成保时捷?谢谢.