|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
我自己写了两个分别如下:
=========================第一个=====================
class Hproperty:
def __get__(self,instance,owner):
return self.info
def __set__(self,instance,value):
self.info = value
Cproperty.info = (value - 32)/1.8
def __del__(self,instance):
del self
class Cproperty:
def __get__(self,instance,owner):
return self.info
def __set__(self,instance,value):
self.info = value
Hproperty.info = (value*1.8 + 32)
def __del__(self,instance):
del self
class Temperature:
c = Cproperty()
h = Hproperty()
===========================第二个======================
class Hproperty:
def __init__(self,fget = None,fset = None,fdel = None):
self.fget = fget
self.fset = fset
self.fdel = fdel
def __get__(self,instance,owner):
return self.fget(instance)
def __set__(self,instance,value):
self.fset(instance,value)
def __delete__(self,instance):
self.fdel(instance)
class Cproperty:
def __init__(self,fget = None,fset = None,fdel = None):
self.fget = fget
self.fset = fset
self.fdel = fdel
def __get__(self,instance,owner):
return self.fget(instance)
def __set__(self,instance,value):
self.fset(instance,value)
def __delete__(self,instance):
self.fdel(instance)
class Temperature:
def __init__(self):
self.cdegree = None
def getH(self):
return (self.cdegree*1.8+32)
def setH(self,value):
self.cdegree = (value-32)/1.8
def delT(self):
del self.cdegree
def getC(self):
return self.cdegree
def setC(self,value):
self.cdegree = value
h = Hproperty(getH,setH,delT)
c = Cproperty(getC,setC,delT)
================================================================
我第一个程序运行时,一开始对摄氏度赋值之后,可以得到正确的华氏度值,但是再赋值或者对华氏度赋值时,发现类的属性会覆盖类的方法,导致无法调用方法,只能读取属性。
我以为是描述符程序只能按照property(fget,fset,fdel)这样的程序来依次传入。但是网课上并没有这样,而是类似我的第一种答案,并且似乎连__delete__方法都没有,但是他的方法并没有被属性覆盖。请问为什么? |
|