当我在 java 类中编写构造函数时,我通常不会在其中调用 super()。当我从 eclipse 源代码编辑器生成构造函数时,为什么它总是在其中添加 super()?
在我编写的构造函数中没有默认添加它是我错了吗?如果我决定使用 eclipse 代码生成器,那么在构造函数中保留 super() 调用有什么问题吗?
最佳答案
正如@Kon 正确指出的那样,无论如何都存在对默认 super 构造函数的隐式调用(这可以通过使用 javap -c
检查字节码轻松验证)。如果您不希望 Eclipse 使其显式化,只需选中构造函数创建 GUI 底部的“省略对默认构造函数 super() 的调用” 复选框即可。
Am I wrong for not adding this by default in the constructors I write?
不,只要您指的是默认 super 构造函数调用 super()
。例如,如果 super 构造函数采用参数,则需要显式调用。
Any thing wrong with leaving the super() call in the constructor if I decide to use eclipse code generator?
不,一点也不。
关于java - 为什么当我使用编辑器代码生成器时,eclipse 会自动在构造函数中添加一个 java super() 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23644520/