英文:
Use generic interface as method argument in Java
问题
让我们假设我有以下接口 Foo
:
abstract public interface Foo {
abstract String getFoo();
}
还有两个扩展 Foo
的类,Bar1
和 Bar2
:
public class Bar1 extends Foo{
String foo = "foobar";
public String getFoo(){
return foo;
}
}
//对于类 Bar2 也类似
我想创建一个翻译器类,其中有一个客户端可以调用的方法,该方法以扩展 Foo
的任何对象为参数(如 Bar1
或 Bar2
),并将字符串翻译为其他字符串。我进行了一些探索,感觉泛型可能是实现这一目标的最佳方法,然而我无法正确地修改方法签名或类签名(不确定是哪个,也许是两者都需要修改?)以实现这种行为。
public class TranslateBar{
//我尝试了以下方法签名,但显然我遗漏了一些东西
public String translateBar(Foo<? extends Foo> pojo){
//返回翻译后的字符串
}
/*
我读到 Object 是所有 Java 类的超类型,所以我想也许将其作为参数,然后使用通配符会起作用,但没有成功
*/
public String translateBar(Object<? extends Foo> pojo){
//返回翻译后的字符串
}
}
在所有情况下,它都会给我一个泛型错误,指出 Type 'java.lang.Object'(或 Foo)does not have type parameters
。它给我修复的两个选项是 create a field for pojo
,但仍然不能解决 <? extends Points2>
错误。
如何使我的 translateBar
方法允许客户端传递 Foo
的任何子类?
英文:
Lets say I have the following interface Foo
abstract public interface Foo {
abstract String getFoo();
}
and two classes that extend Foo
, Bar1
and Bar2
public class Bar1 extends Foo{
String foo = "foobar";
public String getFoo(){
return foo;
}
}
//repeat for class Bar2
I want to create a translator class that had a method that a client can call, which takes any object thats extending Foo as an argument (like Bar1
or Bar2
and translate the string to some other string. I did some digging and feel like generics are going to be the best way to do so, however I'm unable to properly modify either the method signature or the class signatures (not sure which, perhaps both?) to allow for this behavior.
public class TranslateBar{
//I have tried the following signatures, but clearly I'm missing something
public String translateBar(Foo<? extends Foo> pojo>{
//return translated string
}
/*
I read that Object is the supertype for all Java classes, so I thought maybe having it
take an Object in the parameters and then doing the wildcard would work, but no luck
*/
public String translateBar(Object<? extends Foo> pojo>{
//return translated string
}
In all cases, it gives me an error in the generic saying Type 'java.lang.Object'(or Foo) does not have type parameters
. The two options it gives me to fix are create a field for pojo
which still doesn't solve the <? extends Points2>
error.
How can I get my translateBar
method to allow the client to pass any subclass of Foo
?
答案1
得分: 3
在Java中,一个接受特定类型(比如Foo
)的方法也将接受任何Foo
的子类型。在这种情况下,不需要使用泛型。
以下是您的代码应该如何编写:
public interface Foo {
String getFoo();
}
public class Bar1 implements Foo {
final String foo = "foobar";
@Override
public String getFoo(){
return foo;
}
}
public class TranslateBar {
public String translateBar(Foo pojo) {
//返回翻译后的字符串
}
}
现在,您可以使用任何Foo
的实现,包括Bar1
,来调用translateBar
方法:
new TranslateBar().translateBar(new Bar1());
对于不同的情况,您可以使用泛型。例如,在getFoo
方法返回的类型取决于实现的情况下:
// 泛型类型T取决于实现
public interface Foo<T> {
T getFoo();
}
public class Bar1 implements Foo<String> {
final String foo = "foobar";
@Override
public String getFoo(){
return foo;
}
}
public class TranslateBar {
public String translateBar(Foo<?> pojo) {
//返回翻译后的字符串
}
}
英文:
In Java, a method that accepts a certain type, say Foo
, will accept also any sub-type of Foo
. There's no need to use generics in such case.
Here's how your code should look like:
public interface Foo {
String getFoo();
}
public class Bar1 implements Foo {
final String foo = "foobar";
@Override
public String getFoo(){
return foo;
}
}
public class TranslateBar {
public String translateBar(Foo pojo) {
//return translated string
}
}
Now you can call translateBar
with any implementation of Foo
including Bar1
:
new TranslateBar().translateBar(new Bar1());
You would use generic for different cases... for example, where the type returned by the getFoo
method depended on the implementation.
// the type T is generic and depends on the implementation
public interface Foo<T> {
T getFoo();
}
public class Bar1 implements Foo<String> {
final String foo = "foobar";
@Override
public String getFoo(){
return foo;
}
}
public class TranslateBar {
public String translateBar(Foo<?> pojo) {
//return translated string
}
}
答案2
得分: 2
在您的情况下,您不需要使用泛型,基本的多态性将足够。
public String translateBar(Foo pojo){
//返回翻译后的字符串
}
如果您只想调用getFoo()
,这将解决该问题。
英文:
In your case, you don't need to use generics as the basic polymorphism will be sufficient
public String translateBar(Foo pojo){
//return translated string
}
And this will solve the issue if you just want to call the getFoo()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论