每个类对象都是使用相同的new关键字创建的,因此它必须具有关于必须创建对象的类的信息。因此,构造函数的名称应与类名相同。
class MyConstructor{ public MyConstructor() { System.out.println("The constructor name should be same as the class name"); } public static void main(String args[]){ MyConstructor mc = new MyConstructor(); } }
在上面的程序中,构造函数的名称应该与类名相同(MyConstructor)。
The constructor name should be same as the class name
以上就是为什么Java中构造函数的名称与类名相同?的详细内容,更多请关注php中文网其它相关文章!
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号