class People(object):
def init(self,name,age):
self.name=name
self.age=age
def getattribute(self,obj):
if obj == “age”:
print(“问别人年龄是不礼貌的”)
return 18
else:
temp=object.getattribute(self,obj)
return temp
def show(shelf):
print("----")
s=People(“python”,30)
print(s.age)
print(s.name)
s.show()
__getattribute__练习
最新推荐文章于 2025-12-03 12:28:21 发布
该博客内容展示了如何创建一个名为People的类,并重写getattribute方法以控制对特定属性(如年龄)的访问。当尝试获取age属性时,程序会打印提示信息并返回默认值18。此外,还定义了一个show方法来展示对象信息。
2万+

被折叠的 条评论
为什么被折叠?



