简体   繁体   English

Setter 注释在 Java class 中不起作用

[英]Setter annotation is not working in Java class

My workspace structure is like:我的工作区结构如下:

 package1
     Class A
 package 2
    Class B

The class A has all the annotations added to it: class A 添加了所有注释:

@Getter
@Setter
@Value
@Data
@ToString
@Builder
class A {
    int a,
    int b
}

But when I try to use A.setA(1) in class BI get error that setA is not a function defined.但是当我尝试在 class BI 中使用A.setA(1)时,得到错误 setA 不是定义的 function。

I have included the following dependency in config:我在配置中包含了以下依赖项:

LombokUtils = 1.1;
Lombok = 1.16.x; 

Not sure what is getting wrong here.不知道这里出了什么问题。 @Setter annotation is not working. @Setter注释不起作用。

@Getter

This tells lombok to make getters.这告诉 lombok 制造吸气剂。 But, @Data also does this, and @Value also does this.但是,@ @Data也这样做, @Value也这样做。 There is no point to this annotation;这个注释没有意义; remove it.去掉它。

@Setter

This tells lombok to make setters for all non-final fields (spoiler: Your fields are final here, so no setters are made).这告诉 lombok为所有非最终字段创建设置器(剧透:您的字段在这里是最终的,所以没有设置器)。 But, @Data also does this - it also means: Make a setter for every non-final field.但是, @Data也这样做 - 这也意味着:为每个非最终字段创建一个设置器。 There is no point to this annotation;这个注释没有意义; remove it.去掉它。

@Value

This tells lombok to make the class final, to make all fields final , to make all fields private, make a constructor that sets all fields, make a toString, make getters, and make hashcode and equals methods.这告诉 lombok 使 class 成为最终的,使所有字段成为最终的,使所有字段成为私有的,创建一个设置所有字段的构造函数,创建一个 toString,创建 getter,并创建 hashcode 和 equals 方法。

Because it makes all fields final, this makes @Setter do nothing.因为它使所有字段成为最终字段,所以这使得@Setter什么也不做。

@Data

You should not annotate a class with both Data and Value: They are opposites.您不应同时使用数据和值来注释 class:它们是相反的。 If you intend for those fields to be settable, you're looking for @Data , and not for @Value .如果您打算将这些字段设置为可设置,则您正在寻找@Data ,而不是@Value Delete one of them.删除其中之一。

@ToString

Both Data and Value already do this; Data 和 Value 都已经这样做了; There is no point to this annotation;这个注释没有意义; remove it.去掉它。

@Builder

That's not implied by anything else, so keep it.这不是其他任何东西所暗示的,所以请保留它。

I'm pretty sure you want:我很确定你想要:

@Data @Builder class Foo {
}

and nothing else.没有别的了。 Actually, you have builder, so you may want an immutable class (so, no setters at all - you construct an instance and it is then not changable), in which case, you're looking for @Value @Builder class Foo {} .实际上,您有构建器,因此您可能需要一个不可变的 class (因此,根本没有设置器 - 您构建一个实例,然后它就不可更改),在这种情况下,您正在寻找@Value @Builder class Foo {} .

There's a conflict between @Value (which helps creating immutable classes) and @Setter (which adds methods that mutates the class state). @Value (它有助于创建不可变类)和@Setter (它添加了改变 class 状态的方法)之间存在冲突。

Remove the @Value annotation, and you should have setters in class A.删除@Value注释,您应该在 class A 中有设置器。

Based on lombok's reference here :基于龙目岛的参考here

@Value is the immutable variant of @Data; @Value 是 @Data 的不可变变体; all fields are made private and final by default, and setters are not generated.默认情况下,所有字段都是私有的和最终的,并且不会生成设置器。

@Value creates immutable classes while @Setter makes classes mutable by adding setters. @Value 创建不可变类,而 @Setter 通过添加 setter 使类可变。

So in order to have setters remove @Value .因此,为了让 setter 删除 @Value

check this out without @Value:在没有@Value 的情况下检查一下:

package com.samples.demo.pacakge1;

import lombok.*;

@Data
@Getter
@Setter
@Builder
//@Value
@ToString
public class A {
    int x;
    int y;
}

and class B:和 class B:

package com.samples.demo.package2;

import com.samples.demo.pacakge1.A;

public class B {
    public static void main(String[] args) {
        A a = A.builder().build(); //can't use A a=new A();
        a.setY(12);
        System.out.println(a.getY());

    }
}

Is not working becasue in the class A you need to import lombok.Getter;在 class A 中不起作用,您需要import lombok.Getter; and import lombok.Setter;import lombok.Setter; Now in your B class you should be able to access the setter.现在在您的 B class 中,您应该能够访问设置器。 You can get more information Here你可以在这里获得更多信息

change the visibility of the class A as public and try将 class A 的可见性更改为公开并尝试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM