import java.util.Objects;
public class Vehicle {
private final String brand;
private final int wheels;
private final int doors;
private final int maxSpeed;
private Vehicle(Builder builder) {
this.brand = Objects.requireNonNull(builder.brand, "brand");
this.wheels = Objects.requireNonNull(builder.wheels, "wheels");
this.doors = Objects.requireNonNull(builder.doors, "doors");
this.maxSpeed = Objects.requireNonNull(builder.maxSpeed, "maxSpeed");
}
public static Builder builder() {
return new Builder();
}
public void display() {
System.out.println("brand = " + getBrand());
System.out.println("wheels = " + getWheels());
System.out.println("doors = " + getDoors());
System.out.println("maxSpeed = " + getMaxSpeed());
}
public String getBrand() {
return brand;
}
public int getWheels() {
return wheels;
}
public int getDoors() {
return doors;
}
public int getMaxSpeed() {
return maxSpeed;
}
public static class Builder {
private String brand;
private Integer wheels;
private Integer doors;
private Integer maxSpeed;
Builder() {
}
public Builder setBrand(String brand) {
this.brand = brand;
return this;
}
public Builder setWheels(int wheels) {
this.wheels = wheels;
return this;
}
public Builder setDoors(int doors) {
this.doors = doors;
return this;
}
public Builder setMaxSpeed(int maxSpeed) {
this.maxSpeed = maxSpeed;
return this;
}
public Builder of(Vehicle vehicle) {
this.brand = vehicle.brand;
this.wheels = vehicle.wheels;
this.doors = vehicle.doors;
this.maxSpeed = vehicle.maxSpeed;
return this;
}
public Vehicle build() {
return new Vehicle(this);
}
}
}
Vehicle vehicle = new Vehicle.Builder()
.setBrand("Mercedes")
.setWheels(4)
.setDoors(4)
.setMaxSpeed(250)
.build();
public class Vehicle {
private final String brand;
private final int wheels;
private final int doors;
private final int maxSpeed;
private Vehicle(String brand, int wheels, int doors, int maxSpeed){
this.brand = brand;
this.wheels = wheels;
this.doors = doors;
this.maxSpeed = maxSpeed;
}
public Vehicle() {
}
public String getBrand() {
return brand;
}
public int getWheels() {
return wheels;
}
public int getDoors() {
return doors;
}
public int getMaxSpeed() {
return maxSpeed;
}
public Vehicle setBrand(String brand) {
this.brand = brand;
return this;
}
public Vehicle setWheels(int wheels) {
this.wheels = wheels;
return this;
}
public Vehicle setDoors(int doors) {
this.doors = doors;
return this;
}
public Vehicle setMaxSpeed(int maxSpeed) {
this.maxSpeed = maxSpeed;
return this;
}
public void display() {
System.out.println("brand = " + getBrand());
System.out.println("wheels = " + getWheels());
System.out.println("doors = " + getDoors());
System.out.println("maxSpeed = " + getMaxSpeed());
}
}
Vehicle vehicle = new Vehicle();
vehicle.setBrand("Mercedes")
.setDoors(4)
.setMaxSpeed(250);
builder模式与fluent API相比有一些好处:
但这是绝对正常的,如果你不需要这个好处,使用你的方法。有些图书馆使用
我有两个基于同一ecore模型的EMF实例版本。我需要以以下格式准备一个从v1到v2更改的事物列表
Ordering是Guava类库提供的一个犀利强大的比较器工具,Guava的Ordering和JDK Comparator相比功能更强。它非常容易扩展,可以轻松构造复杂的comparator,然后用在容器的比较、排序等操作中。 常用静态方法 Ordering.natural(); // 使用Comparable类型的自然顺序, 例如:整数从小到大,字符串是按字典
问题内容: Comparable和Comparator之间的主要区别是什么。 在什么情况下,哪个比另一个优先? 问题答案: 当你的类实现时,该类的方法将定义该对象的“自然”顺序。根据合同,该方法有义务(尽管不要求)与该对象上的其他方法保持一致,例如,当比较返回true 时,应始终为对象返回0 。 一个比较本身就是如何比较两个对象的定义,可用于可能不与自然顺序排列的方式来比较的对象。 例如,字符串通
Java在进行比较时抛出的错误是: 然后比较的方法(比较器 你知道发生了什么事吗?
你更喜欢/建议哪一个?
问题内容: 我正在阅读Apple开发人员页面上的iOS教程。 在我看来,这和几乎具有相同的功能。 两者之间有什么区别吗? 项目中的不同用法? 更新 是的 ,我看过上面的链接,我仍然不知道什么区别之间使用和。当我问这样的问题时,我希望看到有关该主题的简单说明。有时很难从文档中获取所有内容。 问题答案: 本质上,协议与Java接口非常相似,除了: Swift协议还可以指定必须实现的属性(即字段) Sw