import mathclass Circle:def __init__(self, radius):self.__radius = radius # 设置私有属性,不让用实例.__radius访问def __get_radius(self):return round(self.__radius, 1)def __set_radius(self, radius):if not isinstance(radius, (int, float)):raise TypeError('wronge type')self.__radius = radius@propertydef S(self):return self.__radius ** 2 * math.pi #property第二种方法,加装饰器@property@S.setterdef S(self, s):self.__radius = math.sqrt(s / math.pi) #property第二种方法,加装饰器@S.setterR = property(fget=__get_radius, fset=__set_radius) #property其中一种方法,定义类变量c = Circle(5.712)print(c.R) #调用了get_radius方法获取半径
c.R = 8.886 #调用了set_radius,设置半径
print(c.R)
print('=======================')
print(c.S) #调用S方法获取面积
c.S = 3.14 #设置面积
print(c.R)
print(c.S)==================================================================
5.7
8.9
=======================
248.063284953733
1.0
3.14
详细解决方案
【python】使用property函数为类创建可管理属性fget\fset\fdel
热度:60 发布时间:2023-12-05 13:04:10.0
相关解决方案
- 怎么取<s:property value="news.CONTENT" escape="false"/>内容前几位
- struts2的property标签,碰到Attribute id invalid for tag property according to TLD
- No setter found for property 'sessionFactoroy' in class 'com.iBBS.dao.impl.UserI解决思路
- struts2+spring+hibernate中No property found,该如何处理
- Cannot find any information on property 'cipher' in a bean of type 'xx.JiamiBean解决思路
- <s:property value="G.title"/> 怎么截取返回值的固定长度的字符串
- 请问:org.apache.jasper.JasperException: Cannot find any information on property 'userPhone' in a bean of type 'Bean.Sender'
- 那位高手帮小弟我看下这个有关问题Cannot convert value of type [] to required type [] for property 'ba
- org.hibernate.PropertyAccessException: Null value was assigned to a property of解决办法
- J2ME开发中常见属性(Property)及其功用列表
- spring+ibatis框架下的报错:Bean property 'meatOutInfoBaseDao' is not writable or has.该如何解决
- struts2里头的<s:property>不显示值
- <jsp:setProperty name="person" property=""/>中的"/"解决思路
- There is no WRITEABLE property named 'outboxsize' in class 咋回事
- SSH做添加的过程中出现了错误javax.servlet.ServletException: not-null property references a nu
- how to pass property parameter to pointcut with postsharp?解决办法
- The ConnectionString property has not been initialized. 谁来帮小弟我看看一下,就十几行代码
- The ConnectionString property has not been initialized解决方法
- 蛇年万事大吉!Public Property Name As String = "kkk" & [color=#FF0000]nf.Users.Count[
- ubuntu9.04上安装mic2 出错 (python setup.py build)
- ubuntu9.04下安装mic2 出错 (python setup.py build),该如何解决
- Qt4 设计员 无法生成 python 代码
- 请教如何理解JAVA、C++ Perl、PHP、Ruby、Python
- Qt4 设计师 无法生成 python 代码,该如何解决
- python sybase装配
- python fastcgi怎么获取POST的数据字段
- python(django框架) + mysql 的编码有关问题,求解
- jsp:setProperty property="*"/>问题
- struts2中的<s:property />
- <s:iterator value 有值但<s:property 取不到值