关于设计模式中Builder模式 V2版本中关于内部静态的疑惑
来源:7-2 建造者模式coding
慕斯1452597
2019-09-21
Course.java
public class Course {
private String courseName;
private String coursePPT;
private String courseVideo;
private String courseArticle;
//question & answer
private String courseQA;
public Course buildCourseName(String courseName){
this.courseName = courseName;
return this;
}
public Course buildCoursePPT(String coursePPT) {
this.coursePPT = coursePPT;
return this;
}
public Course buildCourseVideo(String courseVideo) {
this.courseVideo = courseVideo;
return this;
}
public Course buildCourseArticle(String courseArticle) {
this.courseArticle = courseArticle;
return this;
}
public Course buildCourseQA(String courseQA) {
this.courseQA = courseQA;
return this;
}
@Override
public String toString() {
return "Course{" +
"courseName='" + courseName + '\'' +
", coursePPT='" + coursePPT + '\'' +
", courseVideo='" + courseVideo + '\'' +
", courseArticle='" + courseArticle + '\'' +
", courseQA='" + courseQA + '\'' +
'}';
}
}
Test.java
public class Test {
public static void main(String[] args) {
Course course = new Course();
course.buildCourseName("Java设计模式精讲").buildCoursePPT("Java设计模式精讲PPT").buildCourseVideo("Java设计模式精讲视频");
System.out.println(course);
Set<String> set = ImmutableSet.<String>builder().add("a").add("b").build();
System.out.println(set);
}
}
这样去掉static的内部类,和V2版本有内部static类有什么区别,或者说为什么定义个static内部类是出于哪方面的考虑
写回答
1回答
-
geelylucky
2019-09-21
静态内部类就是为了控制访问范围,不让外面的使用者关心里面到底是怎么运行的,只需要使用相关的方法就可以了。
00
相似问题