1 回答

TA貢獻1815條經驗 獲得超13個贊
是的,在靜態語言中,您可以讓您的Profiletake 引用 anAPIConnection并且編譯器將強制執行該接口。
使用 python,您可以進行單元測試,該測試實際通過APIConnection,然后User將捕獲對方法的任何調用。
事實上你可以這樣做:
class User(APIConnection):
def __init__(self, username, password):
super().__init__()
# do login stuff, get access token
# update inherited session with authorization headers
self.session.headers.update({"Access-Token": access_token})
self.profile = Profile(self, profile_data) # set up profile object
class Profile:
def __init__(self, api, profile_data):
self.api = api
def edit_profile(self):
self.api.api_call()
def remove_avatar(self):
self.api.api_call()
添加回答
舉報