在 Java 中使用通用接口作为方法参数
Use generic interface as method argument in Java
假设我有以下界面Foo
abstract public interface Foo {
abstract String getFoo();
}
和两个扩展 Foo
、Bar1
和 Bar2
的 classes
public class Bar1 extends Foo{
String foo = "foobar";
public String getFoo(){
return foo;
}
}
//repeat for class Bar2
我想创建一个转换器 class,它有一个客户端可以调用的方法,它接受任何扩展 Foo 的对象作为参数(比如 Bar1
或 Bar2
和将字符串翻译成其他字符串。我做了一些挖掘,觉得泛型将是最好的方法,但是我无法正确修改方法签名或 class 签名(不确定哪个,也许两个?)允许这种行为。
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
}
在所有情况下,它都会给我一个通用说法 Type 'java.lang.Object'(or Foo) does not have type parameters
的错误。它给我修复的两个选项是 create a field for pojo
,它仍然没有解决 <? extends Points2>
错误。
如何让我的 translateBar
方法允许客户端传递 Foo
的任何子 class?
在你的情况下,你不需要使用泛型,因为基本的多态性就足够了
public String translateBar(Foo pojo){
//return translated string
}
如果您只想调用 getFoo()
,这将解决问题
在 Java 中,接受特定类型的方法,比如 Foo
,也将接受 Foo
的任何 sub-type。在这种情况下不需要使用泛型。
您的代码应如下所示:
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
}
}
现在您可以使用 Foo
的任何实现调用 translateBar
,包括 Bar1
:
new TranslateBar().translateBar(new Bar1());
您可以在不同情况下使用泛型...例如,getFoo
方法返回的类型取决于实现。
// 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
}
}
假设我有以下界面Foo
abstract public interface Foo {
abstract String getFoo();
}
和两个扩展 Foo
、Bar1
和 Bar2
public class Bar1 extends Foo{
String foo = "foobar";
public String getFoo(){
return foo;
}
}
//repeat for class Bar2
我想创建一个转换器 class,它有一个客户端可以调用的方法,它接受任何扩展 Foo 的对象作为参数(比如 Bar1
或 Bar2
和将字符串翻译成其他字符串。我做了一些挖掘,觉得泛型将是最好的方法,但是我无法正确修改方法签名或 class 签名(不确定哪个,也许两个?)允许这种行为。
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
}
在所有情况下,它都会给我一个通用说法 Type 'java.lang.Object'(or Foo) does not have type parameters
的错误。它给我修复的两个选项是 create a field for pojo
,它仍然没有解决 <? extends Points2>
错误。
如何让我的 translateBar
方法允许客户端传递 Foo
的任何子 class?
在你的情况下,你不需要使用泛型,因为基本的多态性就足够了
public String translateBar(Foo pojo){
//return translated string
}
如果您只想调用 getFoo()
在 Java 中,接受特定类型的方法,比如 Foo
,也将接受 Foo
的任何 sub-type。在这种情况下不需要使用泛型。
您的代码应如下所示:
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
}
}
现在您可以使用 Foo
的任何实现调用 translateBar
,包括 Bar1
:
new TranslateBar().translateBar(new Bar1());
您可以在不同情况下使用泛型...例如,getFoo
方法返回的类型取决于实现。
// 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
}
}