Python:面对对象进阶
This commit is contained in:
parent
7d976d446d
commit
7926bea021
195
wiki/programming-language/Python/进阶/语言参考/面向对象进阶.md
Normal file
195
wiki/programming-language/Python/进阶/语言参考/面向对象进阶.md
Normal file
@ -0,0 +1,195 @@
|
||||
---
|
||||
title: 面向对象进阶
|
||||
description: 面向对象进阶
|
||||
keywords:
|
||||
- 面对对象
|
||||
tags:
|
||||
- 进阶
|
||||
sidebar_position: 1
|
||||
author: 7Wate
|
||||
date: 2023-01-16
|
||||
---
|
||||
|
||||
### slots 魔法
|
||||
|
||||
如果我们需要限定自定义类型的对象只能绑定某些属性,可以通过在类中定义__slots__变量来进行限定。需要注意的是__slots__的限定只对当前类的对象生效,对子类并不起任何作用。
|
||||
|
||||
```python
|
||||
class Person(object):
|
||||
|
||||
# 限定Person对象只能绑定_name, _age和_gender属性
|
||||
__slots__ = ('_name', '_age', '_gender')
|
||||
|
||||
def __init__(self, name, age):
|
||||
self._name = name
|
||||
self._age = age
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def age(self):
|
||||
return self._age
|
||||
|
||||
@age.setter
|
||||
def age(self, age):
|
||||
self._age = age
|
||||
|
||||
def play(self):
|
||||
if self._age <= 16:
|
||||
print('%s正在玩飞行棋.' % self._name)
|
||||
else:
|
||||
print('%s正在玩斗地主.' % self._name)
|
||||
|
||||
|
||||
def main():
|
||||
person = Person('王大锤', 22)
|
||||
person.play()
|
||||
person._gender = '男'
|
||||
# AttributeError: 'Person' object has no attribute '_is_gay'
|
||||
# person._is_gay = True
|
||||
```
|
||||
|
||||
### @property 装饰器
|
||||
|
||||
如果想访问属性可以通过属性的 getter(访问器)和 setter(修改器)方法进行对应的操作。要做到这点,就可以考虑使用@property包装器来包装getter和setter方法,使得对属性的访问既安全又方便,代码如下所示。
|
||||
|
||||
```python
|
||||
class Person(object):
|
||||
|
||||
def __init__(self, name, age):
|
||||
self._name = name
|
||||
self._age = age
|
||||
|
||||
# 访问器 - getter方法
|
||||
@property
|
||||
def name(self):
|
||||
return self._name
|
||||
|
||||
# 访问器 - getter方法
|
||||
@property
|
||||
def age(self):
|
||||
return self._age
|
||||
|
||||
# 修改器 - setter方法
|
||||
@age.setter
|
||||
def age(self, age):
|
||||
self._age = age
|
||||
|
||||
def play(self):
|
||||
if self._age <= 16:
|
||||
print('%s正在玩飞行棋.' % self._name)
|
||||
else:
|
||||
print('%s正在玩斗地主.' % self._name)
|
||||
|
||||
|
||||
def main():
|
||||
person = Person('王大锤', 12)
|
||||
person.play()
|
||||
person.age = 22
|
||||
person.play()
|
||||
# person.name = '白元芳' # AttributeError: can't set attribute
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
```
|
||||
|
||||
### @staticmethod 静态方法
|
||||
|
||||
可以使用`@staticmethod`注解定义静态方法,通过直接调用类使用方法。
|
||||
|
||||
```python
|
||||
from math import sqrt
|
||||
|
||||
|
||||
class Triangle(object):
|
||||
|
||||
def __init__(self, a, b, c):
|
||||
self._a = a
|
||||
self._b = b
|
||||
self._c = c
|
||||
|
||||
@staticmethod
|
||||
def is_valid(a, b, c):
|
||||
return a + b > c and b + c > a and a + c > b
|
||||
|
||||
def perimeter(self):
|
||||
return self._a + self._b + self._c
|
||||
|
||||
def area(self):
|
||||
half = self.perimeter() / 2
|
||||
return sqrt(half * (half - self._a) *
|
||||
(half - self._b) * (half - self._c))
|
||||
|
||||
|
||||
def main():
|
||||
a, b, c = 3, 4, 5
|
||||
# 静态方法和类方法都是通过给类发消息来调用的
|
||||
if Triangle.is_valid(a, b, c):
|
||||
t = Triangle(a, b, c)
|
||||
print(t.perimeter())
|
||||
# 也可以通过给类发消息来调用对象方法但是要传入接收消息的对象作为参数
|
||||
# print(Triangle.perimeter(t))
|
||||
print(t.area())
|
||||
# print(Triangle.area(t))
|
||||
else:
|
||||
print('无法构成三角形.')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
```
|
||||
|
||||
### @classmethod 类方法
|
||||
|
||||
Python 还可以在类中定义类方法,类方法的第一个参数约定名为 cls,它代表的是当前类相关的信息的对象(类本身也是一个对象,有的地方也称之为类的元数据对象),通过这个参数我们可以获取和类相关的信息并且可以创建出类的对象,代码如下所示。
|
||||
|
||||
```python
|
||||
from time import time, localtime, sleep
|
||||
|
||||
|
||||
class Clock(object):
|
||||
"""数字时钟"""
|
||||
|
||||
def __init__(self, hour=0, minute=0, second=0):
|
||||
self._hour = hour
|
||||
self._minute = minute
|
||||
self._second = second
|
||||
|
||||
@classmethod
|
||||
def now(cls):
|
||||
ctime = localtime(time())
|
||||
return cls(ctime.tm_hour, ctime.tm_min, ctime.tm_sec)
|
||||
|
||||
def run(self):
|
||||
"""走字"""
|
||||
self._second += 1
|
||||
if self._second == 60:
|
||||
self._second = 0
|
||||
self._minute += 1
|
||||
if self._minute == 60:
|
||||
self._minute = 0
|
||||
self._hour += 1
|
||||
if self._hour == 24:
|
||||
self._hour = 0
|
||||
|
||||
def show(self):
|
||||
"""显示时间"""
|
||||
return '%02d:%02d:%02d' % \
|
||||
(self._hour, self._minute, self._second)
|
||||
|
||||
|
||||
def main():
|
||||
# 通过类方法创建对象并获取系统时间
|
||||
clock = Clock.now()
|
||||
while True:
|
||||
print(clock.show())
|
||||
sleep(1)
|
||||
clock.run()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
```
|
Loading…
Reference in New Issue
Block a user