@ToString(callSuper = true)
时间: 2024-01-17 15:07:00 浏览: 6
This is a Lombok annotation used to generate a toString() method in a class. The callSuper=true parameter indicates that the generated toString() method should also include the output of the toString() method of the superclass of the current class. This can be useful for debugging and logging purposes.
相关问题
@EqualsAndHashCode(callSuper = true) @ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)注解用于子类对象之间进行比较时,会将父类对象的属性也考虑在内,根据父类和子类共同的属性进行比较。这样可以确保子类对象在比较时能够正确地考虑到父类对象的属性。
@ToString(callSuper = true)注解会将父类中的属性也包含在生成的toString方法中。这样可以确保在打印子类对象时,能够同时打印出父类对象的属性。
以下是一个示例代码,演示了@EqualsAndHashCode(callSuper = true)和@ToString(callSuper = true)的使用:
```java
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
@Data
class Parent {
private String parentProperty;
}
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
class Child extends Parent {
private String childProperty;
}
public class Main {
public static void main(String[] args) {
Parent parent = new Parent();
parent.setParentProperty("Parent Property");
Child child1 = new Child();
child1.setParentProperty("Parent Property");
child1.setChildProperty("Child Property");
Child child2 = new Child();
child2.setParentProperty("Parent Property");
child2.setChildProperty("Child Property");
System.out.println(child1.equals(child2)); // 输出:true
System.out.println(child1.toString()); // 输出:Child(parentProperty=Parent Property, childProperty=Child Property)
}
}
```
在上述示例中,Child类继承自Parent类,并使用了@Data、@EqualsAndHashCode(callSuper = true)和@ToString(callSuper = true)注解。通过equals方法比较两个Child对象时,会同时考虑到父类对象的属性。在打印Child对象时,会同时打印出父类对象的属性。
@Data @EqualsAndHashCode(callSuper = false) @Accessors(chain = true) @TableName("country")
这段代码是使用了Lombok和MyBatis-Plus的注解,作用如下:
- `@Data`:自动生成Getter、Setter、`toString()`、`equals()`和`hashCode()`方法。
- `@EqualsAndHashCode(callSuper = false)`:自动生成`equals()`和`hashCode()`方法,`callSuper`属性设置为`false`表示不考虑父类的属性。
- `@Accessors(chain = true)`:生成链式调用的Setter方法,即每个Setter方法返回当前对象,可以连续调用多个Setter方法。
- `@TableName("country")`:将Java类映射到数据库中的表名为`country`,在使用MyBatis-Plus时需要用到。
这些注解可以简化Java代码的编写,提高开发效率。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.png)