为什么 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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP