为 java 中的 class 编写构建器时出现问题
Problem writing a builder for a class in java
我在为 java 中的 class 编写构建器时遇到问题。它总是给我一个错误,说找不到符号。
class Company {
private String name;
private String address;
private String contact;
private int sizeOfEmployee;
private Date createdTime;
@Override
public String toString() {
return "Company{" +
"name='" + name + '\'' +
", address='" + address + '\'' +
", contact='" + contact + '\'' +
", sizeOfEmployee=" + sizeOfEmployee +
", createdTime=" + createdTime +
'}';
}
public static class builder {
private String name;
private String address;
private String contact;
private int sizeOfEmployee;
private Date createdTime;
public builder Name (String name){
this.name = name;
return this;
}
public builder address(String address){
this.address = address;
return this;
}
public builder contact(String contact){
this.contact = contact;
return this;
}
public builder size(int sizeOfEmployee){
this.sizeOfEmployee = sizeOfEmployee;
return this;
}
public builder date(Date createdTime){
this.createdTime = createdTime;
return this;
}
}
}
我想编写一个可以打印出我的公司的构建器 class。谁能告诉我为什么我的生成器不工作?我搜索如何编写构建器并尝试复制该方法。但它在这里不起作用。它给我一个错误,说错误找不到符号。
如果我打电话
Company abc = Company.builder().name("abc").address("Virginia").createdTime(new Date()).sizeOfEmployee(300).contact("12345").build();
会打印出来
Company{name='abc', address='Virginia', contact='12345', sizeOfEmployee=300, createdTime=Wed Oct 21 11:50:41 EDT 2020}
您正在调用不存在的静态方法生成器“Company.builder()”。
你可以这样解决问题:
public class Company {
private String name;
private String address;
public static CompanyBuilder builder() {
return new CompanyBuilder();
}
private static class CompanyBuilder {
private final Company company = new Company();
public CompanyBuilder name(String name){
company.name = name;
return this;
}
public CompanyBuilder address(String address){
company.address = address;
return this;
}
public Company build() {
return company;
}
}
public static void main(String[] args) {
System.out.println(Company.builder().name("name").address("address").build());
}
}
我建议将 Lombok 用于 java 项目。一个注解@Builder包括Lombok插件和依赖你就可以解决这个问题,你也可以使用很多功能而不是样板代码。
import lombok.Builder;
import lombok.ToString;
@Builder
@ToString
public class Company {
private String name;
private String address;
public static void main(String[] args) {
System.out.println(Company.builder().name("name").address("address").build());
}
}
我在为 java 中的 class 编写构建器时遇到问题。它总是给我一个错误,说找不到符号。
class Company {
private String name;
private String address;
private String contact;
private int sizeOfEmployee;
private Date createdTime;
@Override
public String toString() {
return "Company{" +
"name='" + name + '\'' +
", address='" + address + '\'' +
", contact='" + contact + '\'' +
", sizeOfEmployee=" + sizeOfEmployee +
", createdTime=" + createdTime +
'}';
}
public static class builder {
private String name;
private String address;
private String contact;
private int sizeOfEmployee;
private Date createdTime;
public builder Name (String name){
this.name = name;
return this;
}
public builder address(String address){
this.address = address;
return this;
}
public builder contact(String contact){
this.contact = contact;
return this;
}
public builder size(int sizeOfEmployee){
this.sizeOfEmployee = sizeOfEmployee;
return this;
}
public builder date(Date createdTime){
this.createdTime = createdTime;
return this;
}
}
}
我想编写一个可以打印出我的公司的构建器 class。谁能告诉我为什么我的生成器不工作?我搜索如何编写构建器并尝试复制该方法。但它在这里不起作用。它给我一个错误,说错误找不到符号。 如果我打电话
Company abc = Company.builder().name("abc").address("Virginia").createdTime(new Date()).sizeOfEmployee(300).contact("12345").build();
会打印出来
Company{name='abc', address='Virginia', contact='12345', sizeOfEmployee=300, createdTime=Wed Oct 21 11:50:41 EDT 2020}
您正在调用不存在的静态方法生成器“Company.builder()”。 你可以这样解决问题:
public class Company {
private String name;
private String address;
public static CompanyBuilder builder() {
return new CompanyBuilder();
}
private static class CompanyBuilder {
private final Company company = new Company();
public CompanyBuilder name(String name){
company.name = name;
return this;
}
public CompanyBuilder address(String address){
company.address = address;
return this;
}
public Company build() {
return company;
}
}
public static void main(String[] args) {
System.out.println(Company.builder().name("name").address("address").build());
}
}
我建议将 Lombok 用于 java 项目。一个注解@Builder包括Lombok插件和依赖你就可以解决这个问题,你也可以使用很多功能而不是样板代码。
import lombok.Builder;
import lombok.ToString;
@Builder
@ToString
public class Company {
private String name;
private String address;
public static void main(String[] args) {
System.out.println(Company.builder().name("name").address("address").build());
}
}