Python练习-高级面向对象(二)

这篇博客深入探讨Python中的__slots__特性,旨在优化内存使用,以及@property装饰器的运用,如何实现属性访问控制和数据验证。
摘要由CSDN通过智能技术生成

本部分学习__slots__和@property相关用法,详见代码
 

# 1. 使用__slots__,限制允许动态绑定到实例上的属性或方法
from types import MethodType


class Student():
    # 使用__slot__ 限制绑定属性,只允许绑定如下属性或者方法。
    __slots__ = ("age", "set_age")
    pass


# 给实例绑定属性,对其他实例无效
stu1 = Student()
stu1.age = 18
print(stu1.age)
stu2 = Student()
# print(stu2.age),会报错,
# 类绑定属性,所有实例都有
Student.type = "Pupil"
print(stu2.type)


def set_age(self, age):
    self.age = age


# 给实例绑定方法
stu2.set_age = MethodType(set_age, stu2)
stu2.set_age(25)
print(stu2.age)


# 测试子类__slots__
class GraduateStudent(Student):
    __slots__ = ("name")


g_stu1 = GraduateStudent()
g_stu1.name = "G_Stu_name"
print(g_stu1.name)
# stu1.name = "Name" 会报错
# 可允许绑定范围为子类和父类并集,若父类无限制,实际等于子类可以任意绑定
g_stu1.age = 11
print(g_stu1.age)


# 2. 使用@property,加在方法上,使get set方法变成属性。使类似stu1.score这样直接的属性赋值走方法,实现封装和校验等需求
class StudentPro():

    @property
    def score(self):
        return self.__score

    @score.setter
    def score(self, value):
        if value > 100:
            print("Invalid score value.", value, "Score must be between 0 and 100.")
        self.__score = value


stu_pro_one = StudentPro()
stu_pro_one.score = 99
print(stu_pro_one.score)
stu_pro_one.score = 101


# 练习 请利用@property给一个Screen对象加上width和height属性,以及一个只读属性resolution
class Screen(object):

    @property
    def width(self):
        return self.__width

    @width.setter
    def width(self, value):
        self.__width = value

    @property
    def height(self):
        return self.__height

    @height.setter
    def height(self, value):
        self.__height = value

    @property
    def resolution(self):
        return self.__width * self.__height

# 测试:
s = Screen()
s.width = 1024
s.height = 768
print('resolution =', s.resolution)
if s.resolution == 786432:
    print('测试通过!')
else:
    print('测试失败!')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值