gpt4 book ai didi

language-agnostic - 对接口(interface)编程,通用性有多通用?

转载 作者:行者123 更新时间:2023-12-01 07:32:35 25 4
gpt4 key购买 nike

关闭。这个问题需要更多 focused .它目前不接受答案。












想改进这个问题?更新问题,使其仅关注一个问题 editing this post .

6年前关闭。




Improve this question




返回对象时,我希望将实现层次结构备份多远?

以Java集合接口(interface)为例,这样合适吗?

Collection outerMethod() {
return innerMethod();
}

List innerMethod() {
List list = new ArrayList();
//do something with the list that requires a method in the List interface
return list;
}

或者您想使用 List 作为外部方法的返回值吗?

另一个例子,
List outerMethod() {
List list = innerMethod();
//do something with the list that requires a method in the List interface
return list;
}

Collection innerMethod() {
return new ArrayList();
}

带参数的示例,
void outerMethod() {
innerMethod((List) innerMethodTwo);
}

void innerMethodOne(List list) {
//do something with the list
}

Collection innerMethodTwo() {
return new ArrayList();
}

任何人都可以提供任何一般性建议吗?

最佳答案

您的返回类型应尽可能具体,以便为您的方法的使用者提供最大的灵 active 。本着同样的精神,让任何参数的类型尽可能抽象也是最佳实践,这样您的方法的使用者在他们可以传入的类型方面也有更大的灵 active 。

关于language-agnostic - 对接口(interface)编程,通用性有多通用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/982694/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com