当你初始化没有构造函数的类时,调用什么? [英] What gets called when you initialize a class without a constructor?
本文介绍了当你初始化没有构造函数的类时,调用什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以当一个类有一个私有构造函数,你不能初始化它,但是当它没有一个构造函数,你可以。那么当你初始化一个没有构造函数的类时,什么是调用?
例如,这里调用的是什么b
public class a {
public static void main(String args []){
b classB = new b
}
}
public class b {
public void aMethod(){
}
}
解决方案在Java中没有一个没有构造函数的类 em> 构造函数在编译器自动添加一个默认的类文件:
public ClassName (){
super();
}
如果超类没有公开,或受保护的无参构造函数本身。
So when a class has a private constructor you can't initialize it, but when it doesn't have a constructor you can. So what is called when you initialize a class without a constructor?
As example, what is called here (new b())??
public class a { public static void main(String args[]) { b classB = new b(); } } public class b { public void aMethod() { } }
解决方案There's no such thing as a "class without a constructor" in Java - if there's no explicit constructor in the source code the compiler automatically adds a default one to the class file:
public ClassName() { super(); }
This in turn can fail to compile if the superclass doesn't have a public or protected no-argument constructor itself.
这篇关于当你初始化没有构造函数的类时,调用什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文