gpt4 book ai didi

java - java中的ResultTransformer接口(interface)

转载 作者:行者123 更新时间:2023-11-30 03:23:34 25 4
gpt4 key购买 nike

hibernate中有如下界面。

package org.hibernate.transform;

public interface ResultTransformer extends Serializable {
public Object transformTuple(Object[] tuple, String[] aliases);

public List transformList(List collection);
}

在我的项目中,我需要对结果进行一些转换,这与 hibernate数据库 无关。我想创建一个像这样的界面

public interface ResultTranformer<T>{
public T transform(T t);
}

并实现它。

为了不创建另一个接口(interface),是否可以实现hibernate为我们提供的接口(interface)?它会误导其他开发人员吗?

最佳答案

您可以使用java.util.function.Function为此目的的接口(interface),它的目的是改变一些东西:

Function<YourType, YourType> transformer = new Function<YourType, YourType>() {
@Override YourType apply(YourType input) {
// perform transformation
}
}

或者简单地通过 lambda 表达式

Function<YourType, YourType> transformer = (YourType input) -> {
// perform transformation
}

拥有与库中的名称相同的接口(interface)可能会让其他人感到困惑。

关于java - java中的ResultTransformer接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30773178/

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