Class¶
类定义¶
class Presenter():
def __init__(self, name):
# Constructor
self.name = name
def say_hello(self):
# method
print('Hello, ' + self.name)
实例化¶
presenter = Presenter('Chris')
更改字段的值¶
presenter.name = 'Christopher'
调用对象的方法¶
presenter.say_hello()
访问权限¶
EVERYTHING is public.
_ (single underscore) means avoid unless you really know what you're doing.
__ (double underscore) means do not use.
实践示例¶
class Presenter():
def __init__(self, name):
# Constructor
self.name = name
@property
def name(self):
print('In the getter')
return self.__name
@name.setter
def name(self, value):
print('In setter')
# cool validation here
self.__name = value
presenter = Presenter('Chris')
presenter.name = 'Christopher'
print(presenter.name)
# 输出如下
# In setter
# In setter
# In the getter
# Christopher