gpt4 book ai didi

java - 如何在 Java 中使用 lombok 的 @Delegate 注解

转载 作者:行者123 更新时间:2023-12-05 00:45:55 28 4
gpt4 key购买 nike

我想在我的代码中使用 lombok 的 @Delegate 注释。请检查下面的代码片段,它会抛出一个错误:getAge() is already defined :

public interface I {
String getName();
int getAge();
}

@Data
public class Vo {
private String name;
private long age;
}

@AllArgsConstructor
public class Adapter implements I {

@Delegate(types = I.class)
private Vo vo;

//I want to use my own code here,Because vo.getAge() returns a long,But I.getAge() expects a int
public int getAge(){
return (int) vo.getAge();
}
}

最佳答案

来自 Lombok documentation :

To have very precise control over what is delegated and what isn't, write private inner interfaces with method signatures, then specify these private inner interfaces as types in

@Delegate(types=PrivateInnerInterfaceWithIncludesList.class, excludes=SameForExcludes.class).

这意味着要包含 I 中的所有内容,但只排除 getAge,您可以像这样声明一个额外的内部接口(interface):

private interface Exclude {
int getAge();
}

并将其传递给 exclude:

@Delegate(types = I.class, excludes = Exclude.class)

关于java - 如何在 Java 中使用 lombok 的 @Delegate 注解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62441188/

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