#! /usr/bin/env python #! -*- cording:utf-8 -*- # # class C: # def __getattribute__(self, name): # print("getattribute") # return super().__getattribute__(name)#super默认找父类 # def __getattr__(self, name): # print("getattr") # def __setattr__(self, name, value): # print("setatter") # super().__setattr__(name,value) # def __delattr__(self, name): # print("delattr") # super().__delattr__(name) # c=C() # c.x # # c.x=1 class rectangle: def __init__(self,width=0,height=0):#构造函数 self.width=width self.height=height def __setattr__(self, name, value): if name=="square": self.width=value self.height=value else: super().__setattr__(name,value) def getArea(self): return self.width*self.height r1=rectangle(4,5)#实例化类 print(r1.getArea()) r1.square=10#说明赋予了一个square属性,被设置了,则直接把square赋值给value,执行操作,就是正方形 print(r1.getArea()) print(r1.__dict__)#直接打印出了字典属性
时间: 2024-10-12 13:54:09