【发布时间】:2017-03-31 03:16:43
【问题描述】:
我有很多这样的代码,除了类型PositionJson之外都一样,可能是AnotherJson或FooJson或BarJson
有什么方法可以将所有这些代码提取到一个函数中,然后我可以以某种方式将类型传递给它?这样我的课堂上就不会有几块几乎相同的代码?
我不确定这是否可能,只是想我会问,因为这样做会很好......
/**
* @return the _open_ [PositionJson]s
*/
val positions: Array<PositionJson>?
@Throws(AccountsAPIException::class)
get() {
val service = constructServiceURL(POSITIONS, null, true)
try {
val messageJson = mapper.readValue<MessageJson<Array<PositionJson>>>(
callURL(service),
object: TypeReference<MessageJson<Array<PositionJson>>>() {
})
val error = messageJson.error
if (error != null) throw AccountsAPIException(error.errorCode, error.description)
return messageJson.data
} catch (e: Exception) {
throw AccountsAPIException(e)
}
}
【问题讨论】: