[英]How do you call a method of a generic type in java?
I am trying to make a method which takes in a variable object type as a parameter, and then updates a database depending on what object was passed, a bit like how Petapoco works for C#.我正在尝试创建一个方法,它将变量对象类型作为参数,然后根据传递的对象更新数据库,有点像 Petapoco 对 C# 的工作方式。 For this I am trying to use a generic method in which the object that is passed passes a number of checks before it is inserted/updated into its relevant table.
为此,我尝试使用通用方法,其中传递的对象在插入/更新到其相关表之前通过了许多检查。 A number of questions on Stack Overflow point me towards using a method signature like this:
Stack Overflow 上的许多问题都指向我使用这样的方法签名:
public <T> void upsertObject(Class<T> objectClass, T actualObject) { }
Within the method I would like to be able to call methods like actualObject.getId()
but the compiler needs to know the actual type of the object to call its relevant methods.在方法中,我希望能够调用像
actualObject.getId()
这样的方法,但编译器需要知道对象的实际类型才能调用其相关方法。 Is there any way around this?有没有办法解决? How would I go about achieving these calls?
我将如何实现这些呼叫?
EDIT:编辑:
Edited for more clarity编辑为更清晰
After adding an interface with which to bind T, I found I was getting the error:添加一个用于绑定 T 的接口后,我发现我收到了错误:
"Cannot make a static reference to the non-static method getId() from the type CycleComponent" “无法从 CycleComponent 类型中静态引用非静态方法 getId()”
The method now looks like this, the error line is under T.getId()该方法现在看起来像这样,错误行在 T.getId() 下
public <T extends CycleComponent> void upsertObject(Class<T> objectClass, T fitProObject) {
if (objectClass.getName() == "Cycle") {
if (isInserted(T.getId(), "Cycles")) {
// update the database
}else {
// insert into the database
}
}
}
The interface looks like:界面看起来像:
public interface CycleComponent {
String getId();
}
And the method in the Cycle class looks like: Cycle 类中的方法如下所示:
public String getId() {
return this.cycleId;
}
You can do it with generic type bounds.您可以使用泛型类型边界来实现。
Define:定义:
public <T extends SomeType> void upsertObject(Class<T> objectClass, T actualObject) { }
Where SomeType
is a class or interface, and you'll be able to call methods of SomeType
on the actualObject
instance.凡
SomeType
是一个类或接口,你就可以调用的方法SomeType
在actualObject
实例。
This would limit this method to being used only with types that extend SomeType
(if it's a class) or implement SomeType
(if it's an interface).这将限制此方法仅用于扩展
SomeType
(如果它是一个类)或实现SomeType
(如果它是一个接口)的类型。
For example:例如:
public interface SomeType {
String getId();
}
And:和:
public <T extends SomeType> void upsertObject(Class<T> objectClass, T actualObject) {
if (actualObject.getId() != null) {
...
}
}
Assuming this doesn't have to be absolutely generic (for example, it probably doesn't make sense to upsert a java.lang.Integer
, right?), I'd define an interface with all the methods you need, and add that to the generic classification:假设这不必是绝对通用的(例如,向上插入
java.lang.Integer
可能没有意义,对吗?),我将定义一个包含您需要的所有方法的接口,并添加通用分类:
public interface Upsertable {
int getID();
// Other methods you may need...
}
public <T extends Upsertable> void upsertObject(Class<T> objectClass, T actualObject) { }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.