gpt4 book ai didi

java - 匿名方法使用泛型值作为参数

转载 作者:行者123 更新时间:2023-11-30 04:28:30 26 4
gpt4 key购买 nike

我坚持使用匿名方法的java传统,我正在使用具有通用接口(interface)的第三方库,该接口(interface)将 table_name 作为其通用类型

喜欢

TableQueryCallback<WYF_Brands> =new TableQueryCallback<WYF_Brands>() {
@Override
public void onCompleted(List<WYF_Brands> arg0, int arg1,
Exception arg2, ServiceFilterResponse arg3) {
// TODO Auto-generated method stub
}
};

此处 WYF_Brands 是我的表名称。

我想要的是

TableQueryCallback<WYF_Users> =new TableQueryCallback<WYF_Users>() {
@Override
public void onCompleted(List<WYF_Users> arg0, int arg1,
Exception arg2, ServiceFilterResponse arg3) {
// TODO Auto-generated method stub
}
};

其中 WYF_Users 是我的另一个表。

要求:我想对我的所有表使用这样的方法,但以可重用的方式。

我在数据库中有很多表,并且不愿意为不同的表创建不同的方法。我不知道如何使用可以接受任何表名作为参数的泛型。

另一件事是,该接口(interface)是第三方库的一部分,因此我无法更改它,因为它位于可执行 jar 文件内。

我使用java作为编程语言。

最佳答案

听起来您只想要一个通用方法:

public <T> TableQueryCallback<T> createTableQueryCallback() {
return new TableQueryCallback<T>() {
@Override
public void onCompleted(List<T> list, int arg1,
Exception arg2, ServiceFilterResponse arg3) {
// I'm assuming the implementation here would be the same each time?
}
};
}

尽管我很想创建一个命名嵌套类:

private static SomeSpecificTableQueryCallback<T> implements TableQueryCallback<T> {
@Override
public void onCompleted(List<T> list, int arg1,
Exception arg2, ServiceFilterResponse arg3) {
// I'm assuming the implementation here would be the same each time?
}
}

...我不明白为什么匿名会给您带来任何好处。

关于java - 匿名方法使用泛型值作为参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15224129/

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