什么是非静态初始化块的替代方法? [英] What is the alternative to a non-static initialization block?
问题描述
我的专案有一些喜欢非静态初始化块的开发人员。
/ em> ,以及此替代方案的缺点是什么?我会猜测:初始化 构造函数中的值
非初始化块 根据我的理解,初始化块用于在实例化类时设置值。是一个构造函数然后不够?
public class BlockTest {
String test = new String();
//非静态初始化块
{
test =testString;
}
}
这个块让我困惑,导致可读性降低。感谢您的回应!
首先,没有意义的初始化一个新的String ,因为初始化块立即将它分配给别的东西。无论如何...
一种替代方法是在声明中初始化:
public class BlockTest {
String test =testString;
}
另一个是在构造函数中:
public class BlockTest {
String test;
public BlockTest(){
test =testString
}
}
这两个主要的常见选项。 p>
初始化块有两个主要用途。第一个是对于在初始化期间必须执行一些逻辑的匿名类:
new BaseClass(){
List< String> strings = new ArrayList< String>();
{
strings.add(first);
strings.add(second);
}
}
第二个是通用初始化,构造函数:
public class MediocreExample {
List< String> strings = new ArrayList< String>();
{
strings.add(first);
strings.add(second);
}
public MediocreExample(){
...
}
public MediocreExample(int parameter){
...
} b $ b}
但是,在这两种情况下都有不使用初始化块的替代方法: / p>
new BaseClass(){
List< String> strings = createInitialList();
private List< String> createInitialList(){
List< String> a = new ArrayList< String>();
a.add(first);
a.add(second);
return a;
}
}
和:
public class MediocreExample {
List< String>字符串;
private void initialize(){
strings = new List< String>();
strings.add(first);
strings.add(second);
}
public MediocreExample(){
initialize();
...
}
public MediocreExample(int parameter){
initialize();
...
}
}
做这些事情,使用最适当的方式,并提供最清晰和最容易维护的代码。
My projects had some developer who loved a non-static initialization block.
What is the alternative to this and what is the downside of this alternative? I would guess: initialize the values in the constructor ?
Why should we use a non-initialization block? As far as I understood the "initialization block" is used to set values when instantiating the class. Is a constructor then not sufficient?
public class BlockTest {
String test = new String();
//Non-static initialization block
{
test = "testString";
}
}
This block confuses me and lead to reduced readability. Thanks for your response!
First of all, it doesn't make sense to initialize test to a new String() there, since the initialization block immediately assigns it to something else. Anyways...
One alternative is initializing at the declaration:
public class BlockTest {
String test = "testString";
}
Another is in the constructor:
public class BlockTest {
String test;
public BlockTest () {
test = "testString";
}
}
Those are the two main, common options.
There are two main uses for an initialization block. The first is for anonymous classes that have to perform some logic during initialization:
new BaseClass () {
List<String> strings = new ArrayList<String>();
{
strings.add("first");
strings.add("second");
}
}
The second is for common initialization that must happen before every constructor:
public class MediocreExample {
List<String> strings = new ArrayList<String>();
{
strings.add("first");
strings.add("second");
}
public MediocreExample () {
...
}
public MediocreExample (int parameter) {
...
}
}
However, in both cases there are alternatives that do not use the initialization block:
new BaseClass () {
List<String> strings = createInitialList();
private List<String> createInitialList () {
List<String> a = new ArrayList<String>();
a.add("first");
a.add("second");
return a;
}
}
And:
public class MediocreExample {
List<String> strings;
private void initialize () {
strings = new List<String>();
strings.add("first");
strings.add("second");
}
public MediocreExample () {
initialize();
...
}
public MediocreExample (int parameter) {
initialize();
...
}
}
There are many ways to do these things, use the way that is most appropriate and provides the clearest and most easily maintainable code.
这篇关于什么是非静态初始化块的替代方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!