到目前为止,我已经看到了两个单身人士的例子。
普通单身人士,
public class Singleton {
private static Singleton instance;
static {
instance = new Singleton();
}
private Singleton() {
// hidden constructor
}
public static Singleton getInstance() {
return instance;
}
}
Run Code Online (Sandbox Code Playgroud)
和懒惰的单身人士,
public class Singleton {
private Singleton() {
// hidden constructor
}
private static class Holder {
static final Singleton INSTANCE = new Singleton();
}
public static Singleton getInstance() {
return Holder.INSTANCE;
}
}
Run Code Online (Sandbox Code Playgroud)
编码来自这个线程和这个用户。我最近才开始尝试学习单身人士,因为我以前的方法是
1.) 使用静态来创建类似......
static MyClass instance;
Run Code Online (Sandbox Code Playgroud)
2.) 我会尝试以一种看似奇怪的方式传递一个实例,
MyClass …Run Code Online (Sandbox Code Playgroud) 基本上,我想要的是创建Button一个裁剪图形.在意义上裁剪,Image在后面,并且Button是一个洞,显示出来Graphic.截至目前,它看起来像
但是我希望图形适合按钮,即使它更大,也只是被切断了.我目前的代码是沿着的
Image image = new Image(Main.class.getResource("/texture.png").toExternalForm());
yesButton.setGraphic(new ImageView(image));
Run Code Online (Sandbox Code Playgroud)