慢就是快12-2三种访问权限|getter与setter|魔法方法

三种访问权限

python 复制代码
class Person:
    def __init__(self, name, age, idcard):
        self.name = name #公有属性:当前类中、子类中、类外部,都可以访问
        self._age = age #受保护的属性:当前类中、子类中,都可以访问
        self.__idcard = idcard #私有属性:仅能在当前类中访问

    def speak(self):
        print(f'我叫{self.name},年龄是{self._age},身份证:{self.__idcard}')

class Student(Person):
    def hello(self):
        print(f'我是学生{self.name}-{self._age}')
        # idcard 是私有属性:仅能在当前类中访问
        # print(f'我是学生{self.name}-{self._age}-{self.__idcard}')

p1 = Person('哈哈', 18, '354648955462145889')
print(p1.__dict__) # python底层是通过重命名的方式,实现私有属性的
print(p1.name)
# 在类的外部,访问【受保护的属性】,能访问到,但不推荐
# print(p1._age)
# 在类的外部,访问【私有属性】,不能访问到,而且会报错
# print(p1.__idcard)
s1 = Student('梨花', 16, '2026001')
s1.hello()

getter与setter

python 复制代码
class Person:
    def __init__(self, name, age, idcard):
        self.name = name #公有属性:当前类中、子类中、类外部,都可以访问
        self._age = age #受保护的属性:当前类中、子类中,都可以访问
        self.__idcard = idcard #私有属性:仅能在当前类中访问
    # 注册age属性getter方法,当访问Person实例的age属性时,下面的age方法就会被自动调用
    @property
    def age(self):
        return self._age

    # 注册age属性setter方法,当修改Person实例的age属性时,下面的age方法就会被自动调用
    @age.setter
    def age(self,value):
        if value<=120:
            self._age=value
        else:
            print('年龄非法,修改失败')

    @property
    def idcard(self):
        return self.__idcard[:6]+'******'+self.__idcard[-4:]
    @idcard.setter
    def idcard(self,value):
        print('身份证号码不允许修改')


p1 = Person('哈哈', 18, '354648955462145889')
print(p1.name)
print(p1.age)
p1.age=99
print(p1.age)

魔法方法

python 复制代码
# 概念:以__xxx__命名的特殊方法(双下划线开头和结尾)
# 特点:不需要我们手动调,我们只要准备好这些方法,Python会在特定场景下,去自动调用。
class Person:
    def __init__(self, name, age, gender):
        self.name = name
        self.age = age
        self.gender = gender

    # 当执行print(Person的实例对象)或str(Person的实例对象)时调用
    def __str__(self):
        return f'{self.name}-{self.age}-{self.gender}'

    # 当执行len(Person的实例对象)时调用
    def __len__(self):
        return len(p1.__dict__)

    # 当执行 Person的实例对象1 < Person的实例对象2 时调用
    def __lt__(self, other):
        return self.age < other.age

    # 当执行 Person的实例对象1 == Person的实例对象2 时调用
    def __eq__(self, other):
        return self.__dict__ == other.__dict__

    # 当执行 Person的实例对象 不存在的属性 时调用
    def __getattr__(self, item):
        return f'{item}属性不存在'


p1 = Person('哈哈', 18, '男')
p2 = Person('打打', 18, '女')
print(p1)
print(p2)
# res1=str(p1)
# res2=str(p2)
# print(res1)
# print(res2)
print(len(p1))
print(p1 < p2)
print(p1 > p2)
print(p1 == p2)
相关推荐
benchmark_cc1 小时前
一次获取多只股票实时行情:QuantDash `quotes.get(symbols=[...])` 批量快照
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash
geovindu2 小时前
python: PCA
开发语言·后端·python·算法
AI模型调用笔记2 小时前
流式 API 的 usage 为 null?用 cURL 看尾帧,别漏空 choices
python
__万波__2 小时前
嵌入式存储技术--1.Flash物理存储
java·开发语言
Chen_harmony3 小时前
十六、ALV树
开发语言·c++
qq_274475153 小时前
第 28 篇:Promise 全解析:状态、then/catch/finally、静态方法 all/race/allSettled/any
开发语言·javascript
天诰3 小时前
SA-T-06 轨迹数据工程 脏数据检测
人工智能·python·算法·语言模型·ai编程
西西弗Sisyphus3 小时前
C++ 最小 Agentic Loop 实现 - 回调函数系列 - 初识回调(5)
开发语言·c++
程序员小杰@3 小时前
Java 统一报文转换 + 签名验签完整介绍
java·开发语言
Java后端的Ai之路5 小时前
Anthropic 官方文章《Building always-on agents》构建指南
开发语言·网络·python