property璇﹁В

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了property璇﹁В相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/turn' title='turn'>turn   浠g爜   return   int   __init__   瀹炵幇   back   module   trace   

涓嬮潰鎴戜滑鏉ョ湅涓€娈礿ava浠g爜锛?/p>

銆€銆€

class Person{
    private name;//鎶妌ame璁句负绉佹湁灞炴€?    private age;//鎶奱ge璁句负绉佹湁灞炴€?    
    public String getName(){
              return name
    }
    public setName(String name){
             this.name=name
    }
    
    public  int getAge(){
             return age
   }
    
   public setAge(int age){
             this.age=age
   }

}


鎴戜滑鍙兘閫氳繃鐩稿簲鍏湁鐨刧et銆乻et鏂规硶鍘昏闂€佽缃睘鎬у€?br>

鍦╬ython涓紝鎴戜滑鍚屾牱鍙互瀵瑰睘鎬ц繘琛岀鏈夊寲锛屼絾鏄紝鎴戜滑涔熷笇鏈涢€氳繃鎻愪緵涓€涓叕鏈夌殑鏂规硶鍘昏闂€佽缃睘鎬у€?/p>

class Person锛?    def __init__(self,name,age):
        self.__name=name#鎶妌ame灞炴€ц涓虹鏈?        self.__age=age#鎶奱ge灞炴€ц涓虹鏈?
p=Person("鍘ㄦ埧涓嶈触",100)
print(p.__name)
print(p.__age)

鎵ц缁撴灉濡備笅锛?br>Traceback (most recent call last):
  File "F:/python/stuDemo/002.py", line 7, in <module>
    print(p.__name)
AttributeError: 鈥楶erson鈥?object has no attribute 鈥榑_name鈥?/span>

灞炴€х鏈夊寲鏃犳硶璁块棶,鎵€浠ユ姤閿?br>

閭f垜浠兘涓嶈兘鍍廽ava涓€鏍锋彁渚涗竴涓叕鍏辩殑鏂规硶鏉ヨ闂€佽缃睘鎬у憿锛熺瓟妗堝綋鐒舵槸鑲畾鐨勩€?br>

class Person:
def __init__(self,name,age):
self.__name=name#鎶妌ame灞炴€ц涓虹鏈?br> self.__age=age#鎶奱ge灞炴€ц涓虹鏈?br>

def getname(self):
return self.__name

def setname(self,name):
self.__name=name

def getage(self):
return self.__age

def setage(self,age):
self.__age=age

p=Person("鍘ㄦ埧涓嶈触",100)
print(p.getname())
p.setname("鐏板お鐙?)
print(p.getname())
print(p.getage())
p.setage(18)
print(p.getage())




鎵ц缁撴灉涓猴細
鍘ㄦ埧涓嶈触
鐏板お鐙?/span>
100
18


閫氳繃涓婇潰鐨勬柟寮忥紝鎴戜滑宸茬粡瀹炵幇浜嗗拰java涓€鏍风殑鍔熻兘锛屼絾鏄湪python涓紝鎴戜滑鎻愪緵浜嗘洿渚挎嵎鐨勬柟寮忥紝灏辨槸閫氳繃@property瑁呴グ鍣ㄧ殑鏂瑰紡锛岄偅涔堝畠鏂逛究鍦ㄥ摢閲屽憿锛屾垜浠竴璧锋潵鐪嬩笅闈唬鐮併€?br>
class Person:
def __init__(self,name,age):
self.__name=name#鎶妌ame灞炴€ц涓虹鏈?br> self.__age=age#鎶奱ge灞炴€ц涓虹鏈?br>
@property
def name(self):
return self.__name

@name.setter
def name(self,name):
self.__name=name

@property
def age(self):
return self.__age

@age.setter
def age(self,age):
self.__age=age

p=Person("鍘ㄦ埧涓嶈触",100)
print(p.name)
p.name="鐏板お鐙?
print(p.name)
print(p.age)
p.age=18
print(p.age)


鎵ц缁撴灉涓猴細

鍘ㄦ埧涓嶈触
鐏板お鐙?br>100
18


閫氳繃瑙傚療鎴戜滑鍙戠幇锛屾晥鏋滀笌涓婇潰涓€姣涗竴鏍凤紝鏈夋湪鏈夛紵
涓嬮潰锛屾垜浠氨鏉ヤ簡瑙d竴涓嬩笅@property銆?br>@property浼氬皢绫绘柟娉曞彉涓哄彧璇诲睘鎬э紝涔熷氨鏄浉褰撲簬java涓殑get鏂规硶銆?br>杩欐牱涓€鏉ワ紝鎴戜滑灏卞彲浠ヨ皟鐢ㄦ暟鎹睘鎬т竴鏍峰幓璋冪敤鍑芥暟灞炴€с€?br>
瀵逛簬涓婇潰鐨勪唬鐮侊紝鎴戜滑杩樺彲浠ラ€氳繃property鍑芥暟鍘诲疄鐜?/pre>
class Person:
def __init__(self,name,age):
self.__name=name#鎶妌ame灞炴€ц涓虹鏈?br> self.__age=age#鎶奱ge灞炴€ц涓虹鏈?br>

def getname(self):
return self.__name

def setname(self,name):
self.__name=name

def getage(self):
return self.__age

def setage(self,age):
self.__age=age

name=property(getname,setname,"杩欐槸name灞炴€х殑璁块棶鍜岃缃?)
age=property(getage,setage,"杩欐槸age灞炴€х殑璁块棶鍜岃缃?)

p=Person("鍘ㄦ埧涓嶈触",100)
print(p.name)
p.name="鐏板お鐙?
print(p.name)
print(p.age)
p.age=18
print(p.age)



鎵ц缁撴灉涓猴細

鍘ㄦ埧涓嶈触
鐏板お鐙?br>100
18
 

p.name 浼氳Е鍙慻etter
p.name=value 浼氳Е鍙憇etter


 



































































以上是关于property璇﹁В的主要内容,如果未能解决你的问题,请参考以下文章

MapReduce涔婼huffle璇﹁В

ConuntDownLatch璇﹁В

wsdl璇﹁В

mysql浜嬪姟闅旂绾у埆/鑴忚/涓嶅彲閲嶅璇?骞昏璇﹁В

璇﹁ВPython涓殑鍚勭杞箟绗n

HTTP keepalive璇﹁В