课程
什么是 Getter 和 Setter?
- Getter:面向对象编程(OOP)中用于从类中访问私有属性的方法。
- Setter:OOP 中用于为类中的私有属性设定值的方法。
私有属性——封装
如果您不熟悉 Python 中的私有属性或私有方法,请阅读这篇 DataCamp 的文章。
来看一下如何在Python中实现一个私有属性。
class SampleClass:
def __init__(self, a):
## private varibale or property in Python
self.__a = a
## getter method to get the properties using an object
def get_a(self):
return self.__a
## setter method to change the value 'a' using an object
def set_a(self, a):
self.__a = a
SampleClass 有三个方法。
-
__init__:用于初始化类的属性或特性。
- __a:这是一个私有属性。
- get_a:用于获取私有属性 a 的值。
- set_a:用于通过对象为 a 设定值。
在Python中,您无法直接访问私有变量。因此我们实现了getter 方法。
来看一下这个类是如何工作的。
## creating an object
obj = SampleClass(10)
## getting the value of 'a' using get_a() method
print(obj.get_a())
## setting a new value to the 'a' using set_a() method
obj.set_a(45)
print(obj.get_a())
10
45
以上就是在Python中实现私有属性、getter 和 setter的方法。Java中也采用了同样的做法。接下来用一种更Pythonic 的方式来实现相同的功能。
class PythonicWay:
def __init__(self, a):
self.a = a
您无需任何 getter、setter 方法来访问或修改属性。可以直接通过属性名访问。
来看一下。
## Creating an object for the 'PythonicWay' class
obj = PythonicWay(100)
print(obj.a)
100
上述两个类有什么区别?
- SampleClass 隐藏了私有属性和方法,实现了 OOP 的封装特性。
- PythonicWay 不隐藏数据,没有实现任何封装特性。
更应该使用哪种方式?
- 这取决于您的需求。如果需要私有属性和方法,可以通过 setter、getter 方法来实现类;否则可以采用常规方式实现。
Property
现在,如果您希望在 SampleClass 中为属性设值时添加一些条件,该怎么办?
假设只有当传入的值为正且为偶数时,才将其赋给属性;否则将其设为 2。
让我们通过修改 SampleClass 中的 set_a() 方法来实现这一点。
class SampleClass1:
def __init__(self, a):
## calling the set_a() method to set the value 'a' by checking certain conditions
self.set_a(a)
## getter method to get the properties using an object
def get_a(self):
return self.__a
## setter method to change the value 'a' using an object
def set_a(self, a):
## condition to check whether 'a' is suitable or not
if a > 0 and a % 2 == 0:
self.__a = a
else:
self.__a = 2
通过创建对象来检查这个类。
## creating an object for the class 'SampleClass1'
obj = SampleClass1(16)
print(obj.get_a())
16
接下来看看如何使用 @property 装饰器来实现上述类。
class Property:
def __init__(self, var):
## initializing the attribute
self.a = var
@property
def a(self):
return self.__a
## the attribute name and the method name must be same which is used to set the value for the attribute
@a.setter
def a(self, var):
if var > 0 and var % 2 == 0:
self.__a = var
else:
self.__a = 2
@property 用于在不使用任何getter 方法的情况下获取私有属性的值。我们需要在返回私有变量的方法前面加上一行 @property。
要为私有变量设值,我们在方法前使用 @method_name.setter,将其用作 setter。
来测试一下 Property 类,看看这些装饰器是否正常工作。
## creating an object for the class 'Property'
obj = Property(23)
print(obj.a)
2
@a.setter 会根据我们在方法中指定的条件为 a 设值。使用 property 的另一种方式是……
class AnotherWay:
def __init__(self, var):
## calling the set_a() method to set the value 'a' by checking certain conditions
self.set_a(var)
## getter method to get the properties using an object
def get_a(self):
return self.__a
## setter method to change the value 'a' using an object
def set_a
(self, var):
## condition to check whether var is suitable or not
if var > 0 and var % 2 == 0:
self.__a = var
else:
self.__a = 2
a = property(get_a, set_a)
将所有 getter 和 setter 方法传给 property,并将其赋给您希望作为类属性使用的变量。
## creating an object for the 'AnotherWay' class
obj = AnotherWay(28)
print(obj.a)
28
将 setter 和 getter 方法设为私有以隐藏它们。来试一下。
class FinalClass:
def __init__(self, var):
## calling the set_a() method to set the value 'a' by checking certain conditions
self.__set_a(var)
## getter method to get the properties using an object
def __get_a(self):
return self.__a
## setter method to change the value 'a' using an object
def __set_a(self, var):
## condition to check whether var is suitable or not
if var > 0 and var % 2 == 0:
self.__a = var
else:
self.__a = 2
a = property(__get_a, __set_a)
来看一下其是否正常工作。
## creating an object for the 'AnotherWay' class
obj = FinalClass(12)
print(obj.a)
12
结论
现在您已经了解了如何在Python中实现封装,以及 setter、getter 与 property 之间的区别。
如果您是 Python 初学者,建议您学习 DataCamp 的免费课程 Introduction to Python。
如果您对本文有任何问题,请在评论区留言。祝编码愉快!