300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > java默认函数参数是什么意思 什么时候必须在Java中使用默认构造函数和参数化构造函数?...

java默认函数参数是什么意思 什么时候必须在Java中使用默认构造函数和参数化构造函数?...

时间:2021-10-07 00:21:53

相关推荐

java默认函数参数是什么意思 什么时候必须在Java中使用默认构造函数和参数化构造函数?...

Many a time I have got an exception saying "the default constructor's implementation is missing". And many a times a parameterized constructor's definition alone does everything. I want to know under which conditions this happens.

解决方案

The compiler doesn't ever enforce the existence of a default constructor. You can have any kind of constructor as you wish.

For some libraries or frameworks it might be necessary for a class to have a default constructor, but that is not enforced by the compiler.

The problem you might be seeing is if you have a class with a custom constructor and you don't have an implicit super() call in your constructor body. In that case the compiler will introduce a call to the super classes default constructor. If the super class doesn't have a default constructor then your class will fail to compile.

public class MyClass extends ClassWithNoDefaultConstructor

public MyClass() {

super(); //this call will be added by the compiler if you don't have any super call here

// if the super class has no default constructor the above line will not compile

// and removing it won't help either because the compiler will add that call

}

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。