課程
/后端開發
/Python
/Python3 進階教程(新版)
想請教一下參考答案中定義修改set方法,要怎么調用呢
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")
舉報
學習函數式、模塊和面向對象編程,掌握Python高級程序設計
3 回答定義調用問題:為什么set方法不起作用。1.一個@classmethod下,set在get后,不行;set在get前,可行。2.分別類定義set和get,可行。
2 回答看不懂,參考答案為什么要這樣編輯?有沒有大神指導一下?
1 回答參考答案是錯誤的,沒有考慮換行符
2 回答參考答案代碼如何創建實例
2 回答我在IDLE中輸入參考答案代碼為什么運行了但是沒有任何顯示呢
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
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())