課程
/后端開發(fā)
/Python
/Python3 進階教程(新版)
想請教一下參考答案中定義修改set方法,要怎么調(diào)用呢
2022-01-07
源自:Python3 進階教程(新版) 2-8
正在回答
使用get
class Animal():
? ? def __init__(self,name,age,location):
? ? ? ? self.__name=name
? ? ? ? self.__age=age
? ? ? ? self.__location=location
? ? def get_info(self):
? ? ? ? return 'name={},\nage={},\nlocation={}'.format(self.__name,self.__age,self.__location)
? ? def set_name(self,name):
? ? def set_age(self,age):
? ? def set_location(self,location):
? ? ? ??
dog=Animal("hashiqi",4,"guangzhou")
print(dog.get_info())
print("")
dog.set_location("tianjin")
舉報
學(xué)習(xí)函數(shù)式、模塊和面向?qū)ο缶幊?,掌握Python高級程序設(shè)計
3 回答定義調(diào)用問題:為什么set方法不起作用。1.一個@classmethod下,set在get后,不行;set在get前,可行。2.分別類定義set和get,可行。
2 回答看不懂,參考答案為什么要這樣編輯?有沒有大神指導(dǎo)一下?
1 回答參考答案是錯誤的,沒有考慮換行符
2 回答參考答案代碼如何創(chuàng)建實例
2 回答我在IDLE中輸入?yún)⒖即鸢复a為什么運行了但是沒有任何顯示呢
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號
2022-05-17
使用get
2022-01-08
class Animal():
? ? def __init__(self,name,age,location):
? ? ? ? self.__name=name
? ? ? ? self.__age=age
? ? ? ? self.__location=location
? ? def get_info(self):
? ? ? ? return 'name={},\nage={},\nlocation={}'.format(self.__name,self.__age,self.__location)
? ? def set_name(self,name):
? ? ? ? self.__name=name
? ? def set_age(self,age):
? ? ? ? self.__age=age
? ? def set_location(self,location):
? ? ? ? self.__location=location
? ? ? ??
dog=Animal("hashiqi",4,"guangzhou")
print(dog.get_info())
print("")
dog.set_location("tianjin")
print(dog.get_info())