Tóm lược. trong hướng dẫn này, bạn sẽ tìm hiểu về trình trang trí thuộc tính Python [@property] và quan trọng hơn là cách thức hoạt động của nó
Giới thiệu về trình trang trí thuộc tính Python
Trong hướng dẫn trước, bạn đã học cách sử dụng lớp thuộc tính để thêm một thuộc tính vào một lớp. Đây là cú pháp của lớp
4Code language: Python [python]
class Person: def __init__[self, name, age]: self.name = name self.age = age
Code language: Python [python]
class property[fget=None, fset=None, fdel=None, doc=None]
Phần sau đây định nghĩa một lớp
5 với hai thuộc tínhCode language: Python [python]
class Person: def __init__[self, name, age]: self.name = name self.age = age
6 vàCode language: Python [python]
class Person: def __init__[self, name, age]: self.name = name self.age = age
7Code language: Python [python]
class Person: def __init__[self, name, age]: self.name = name self.age = age
Code language: Python [python]
class Person: def __init__[self, name, age]: self.name = name self.age = age
Để định nghĩa một getter cho thuộc tính
7, bạn sử dụng lớpCode language: Python [python]
class Person: def __init__[self, name, age]: self.name = name self.age = age
4 như thế này________số 8_______
class Person: def __init__[self, name, age]: self.name = name self.age = age
Code language: Python [python]
4 chấp nhận một getter và trả về một đối tượng thuộc tínhCode language: Python [python]
class Person: def __init__[self, name, age]: self.name = name self.age = age
Phần sau đây tạo một thể hiện của lớp
5 và nhận giá trị của thuộc tínhCode language: Python [python]
class Person: def __init__[self, name, age]: self.name = name self.age = age
7 thông qua thể hiệnCode language: Python [python]
class Person: def __init__[self, name, age]: self.name = name self.age = age
Code language: Python [python]
john = Person['John', 25] print[john.age]
đầu ra
Code language: Python [python]
25
Ngoài ra, bạn có thể gọi trực tiếp phương thức
3 của đối tượngCode language: Python [python]
class Person: def __init__[self, name, age]: self.name = name self._age = age def get_age[self]: return self._age age = property[fget=get_age]
5 như thế này
class Person: def __init__[self, name, age]: self.name = name self.age = age
Code language: Python [python]
Code language: Python [python]
print[john.get_age[]]
Vì vậy, để lấy
7 của một đối tượngCode language: Python [python]
class Person: def __init__[self, name, age]: self.name = name self.age = age
5, bạn có thể sử dụng thuộc tínhCode language: Python [python]
class Person: def __init__[self, name, age]: self.name = name self.age = age
7 hoặc phương thứcCode language: Python [python]
class Person: def __init__[self, name, age]: self.name = name self.age = age
3. Điều này tạo ra một sự dư thừa không cần thiếtCode language: Python [python]
class Person: def __init__[self, name, age]: self.name = name self._age = age def get_age[self]: return self._age age = property[fget=get_age]
Để tránh sự dư thừa này, bạn có thể đổi tên phương thức
3 thành phương thứcCode language: Python [python]
class Person: def __init__[self, name, age]: self.name = name self._age = age def get_age[self]: return self._age age = property[fget=get_age]
0 như thế nàyCode language: Python [python]
john = Person['John', 25] print[john.age]
class Person: def __init__[self, name, age]: self.name = name self._age = age def age[self]: return self._age age = property[fget=age]
Code language: Python [python]
1 chấp nhận một giá trị có thể gọi được [tuổi] và trả về một giá trị có thể gọi được. Vì vậy, nó là một vật trang trí. Do đó, bạn có thể sử dụng trình trang tríCode language: Python [python]
john = Person['John', 25] print[john.age]
2 để trang trí phương thứcCode language: Python [python]
john = Person['John', 25] print[john.age]
0 như sauCode language: Python [python]
john = Person['John', 25] print[john.age]
Code language: Python [python]
class Person: def __init__[self, name, age]: self.name = name self._age = age @property def age[self]: return self._age
Vì vậy, bằng cách sử dụng trình trang trí
2, bạn có thể đơn giản hóa định nghĩa thuộc tính cho một lớpCode language: Python [python]
john = Person['John', 25] print[john.age]
trang trí setter
Phần sau đây bổ sung một phương thức setter [_______12_______5] để gán giá trị cho thuộc tính
6 cho lớpCode language: Python [python]
john = Person['John', 25] print[john.age]
5Code language: Python [python]
class Person: def __init__[self, name, age]: self.name = name self.age = age
class Person: def __init__[self, name, age]: self.name = name self._age = age @property def age[self]: return self._age def set_age[self, value]: if value