java - What's wrong with my builder pattern? -
i have problem in realisation of builder pattern. have 2 classes:
package course_2; import java.util.date; public class student { private static int idstart = 0; private final int id = idstart++; private string name; private string surname; private string secondname; private date birthdate; private string address; private string phone; private int course; private int group; public static class builder { // Обязательные параметры private final string name; private final string surname; private final date birthdate; // Необязательные параметры, инициализация по умолчанию private string secondname = ""; private string address = ""; private string phone = ""; private int course = 1; private int group = 1; public builder(string name, string surname, date birthdate) { this.name = name; this.surname = surname; this.birthdate = (date) birthdate.clone(); } public builder secondname(string secondname) { this.secondname = secondname; return this; } public builder address(string address) { this.address = address; return this; } public builder phone(string phone) { this.phone = phone; return this; } public builder course(int course) { this.course = course; return this; } public builder group(int group) { this.group = group; return this; } } private student(builder builder) { this.name = builder.name; this.surname = builder.surname; this.secondname = builder.secondname; this.birthdate = builder.birthdate; this.address = builder.address; this.phone = builder.phone; this.course = builder.course; this.group = builder.group; } } the problem when i'm trying call builder client code:
student studentone = new student.builder("andrue", "booble", /*date variable here*/); i'm getting compiler problem :
error:(24, 30) java: incompatible types: course_2.student.builder cannot converted course_2.student
can me understanding, why happen , how can solve it? thanks!
you need add following builder:
public student build(){ return new student(this); } and call this:
student studentone = new student.builder("andrue", "booble", null).build();
Comments
Post a Comment