Java中如何传递泛型参数
在 Java 中,我有两个类:
Class A
{
public String ID;
public Object Name;
}
Class B
{
public String ID;
public Object Name;
}
我想要一个方法,可以将 A 类或 B 类对象传递给它:
public void SomeMethod(??? arg)
{
String id = arg.ID;
Object name= arg.Name;
}
是否可以将 A 类或 B 类对象传递给此方法?如果是的话,方法的签名怎么写?
我能想到的唯一解决方案是创建一个 A 类和 B 类都实现的接口,其中包含 get 和 set 方法来设置字段 ID 和 Name。那么该方法的签名将是一个类型为接口的参数。我希望也许有一种更简单的方法,可能使用泛型?
In Java I have two classes:
Class A
{
public String ID;
public Object Name;
}
Class B
{
public String ID;
public Object Name;
}
I want to have a method where I can pass it either a Class A or B object:
public void SomeMethod(??? arg)
{
String id = arg.ID;
Object name= arg.Name;
}
Is it possible to pass an object of either class A or B to this method? If so, how is the method's signature written?
The only solution I can think of is to create an interface that both Class A and B implements containing get and set methods to set the fields ID and Name. Then the method's signature would be a parameter whose type is the interface. I was hoping that maybe there is a simpler way, possibly with generics?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要使用具有适当方法签名的接口(或抽象类)是正确的。对于java来说,这两个类是不同的,除了Object之外没有任何共同点。您需要创建一个类层次结构来反映它们之间的共性。
You are correct with needing to use an interface (or an abstract class) with the appropriate method signatures. To java the two class are different with nothing (beside Object) in common. You need to create a class hierarchy refelecting the commonality between them.
使用方法重载。
您可以创建一个接口并让
A
和B
实现它。这实际上取决于您的应用程序。对于小型程序,我会坚持使用方法重载,因为它只会在程序中引入不必要的抽象。对于优先考虑可扩展性的大型应用程序,您可能需要考虑使用接口。假设稍后您要编写也具有
SomeMethod()
的类C
和D
。使用接口使您不必一遍又一遍地检查整个代码并重载适当的方法。如果您确定
A
和B
就是故事的结局,那么就没有必要制作界面。编辑:如果有很多代码需要重复,那么创建一个辅助方法:
Use method overloading.
You could make an interface and have
A
andB
implement it. It really depends on your application. For small programs, I would just stick with method overloading since it just introduces unnecessary abstraction into your program.For larger applications where extensibility is a priority, you may want to consider using an interface. Suppose later on you want to write classes
C
andD
which also haveSomeMethod()
. Using an interface makes it so that you don't have to go through your entire code and overload appropriate methods over and over again.If you know for sure that
A
andB
are the end of the story, then there's no need to make an interface.EDIT: If there's a lot of code to be duplicated, then make a helper method:
您不需要泛型类型。简单的继承就可以完成这项工作
You don't need generic types. Simple inheritance will do the job
泛型旨在提高编译期间的类型安全性。
您所问的问题似乎类似于 C++ 概念或各种其他语言的鸭子类型。
在 Java 中,如果需要对两种不同的类型执行某些操作序列,则需要引入接口或诉诸脚本/反射。
Generics are intended to improve type safety during compilation.
What you are asking about seems to be something akin to C++ concepts or various other languages' duck typing.
In Java, if some sequence of operations need to be performed on two disparate types, you need to introduce an interface or resort to scripting/reflection.
定义两个接口 hasID 和 hasName,然后:
其中 getID 和 getName 定义在各自的接口上。
Define two interfaces, hasID and hasName, and then:
Where getID and getName are defined on their respctive interfaces.