您好,欢迎访问一九零五行业门户网

为什么Java中构造函数的名称与类名相同?

每个类对象都是使用相同的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中构造函数的名称与类名相同?的详细内容。
其它类似信息

推荐信息