在Effective Java中,它提到"与构造函数不同,静态工厂方法在每次调用时都不需要创建新对象".
class Car{
String color;
Boolean spoiler;
public Car(String s){
color=s;
spoiler = false;
}
public static Car redCar(){
return new Car("red");
}
}
Run Code Online (Sandbox Code Playgroud)
在主类:
Car c2 = Car.redCar();
Car c3 = Car.redCar();
Run Code Online (Sandbox Code Playgroud)
c2和c3是不同的对象.我没有得到"每次调用都不需要创建新对象"的上下文.
{ vertx run vertcle1.groovy}
{container.deployVerticle("verticle1.groovy"}
Run Code Online (Sandbox Code Playgroud)
执行这两个语句时后台会发生什么?