时间:2021-07-01 10:21:17 帮助过:3人阅读
# -*- coding: utf-8 -*-
"""
Created on Sun Nov 13 23:19:03 2016
@author: toby
"""
#知识点:用装饰器@property,把方法变成一个特性
class Province:
memo = 'One of China\'s 23 provinces' #静态字段
def __init__(self,name,capital,leadership):
self.Name = name #动态字段
self.Capital = capital #动态字段
self.Leadership = leadership #动态字段
def sports(self): #定义一个动态方法,类不能访问动态方法
print self.Name + 'The sports meeting'
#把方法变成一个特性
@property #自带的装饰器
def Bar(self):
print self.Name
return 'somthing' #也是可以有一个返回值的
#实例化两个对象,对象名分别是:hb、sd
hb = Province('hebei','shjiazhuang','liyang')
sd = Province('shandong','jinan','angshenghui')
#通过对象访问这个属性,把方法的访问形式变成访问字段的访问形式
print hb.Bar以上就是python用装饰器@property把方法变成一个特性实例的详细内容,更多请关注Gxl网其它相关文章!