是什么在构造函数或字段定义实例之间的区别? [英] What is the difference between instantiation in constructor or in field definition?
本文介绍了是什么在构造函数或字段定义实例之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
什么是这之间的区别:
公共类Foo {
私人酒吧酒吧;
公共美孚(){巴=新的酒吧(); }
}
和这样的:
公共类Foo {
私人酒吧酒吧=新的酒吧();
公共美孚(){}
}
解决方案
不同的是,在第二种情况下字段初始化此之前发生/基层构造,而在第一种情况下初始化的构造内发生
What is the difference between this:
public class Foo {
private Bar bar;
public Foo() { bar = new Bar(); }
}
and this:
public class Foo {
private Bar bar = new Bar();
public Foo() { }
}
解决方案
The difference is that in the second case field initialization happens before this/base constructor while in the first case initialization happens inside the constructor.
这篇关于是什么在构造函数或字段定义实例之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文