为什么构造函数名称与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
热门推荐
10 新年门口花束祝福语简短
11 盘锦结婚祝福语大全简短
12 父母生日祝福语 简短独特
13 家庭恩爱祝福语简短英文
14 高考俄语祝福语大全简短
15 虎年祝福语 诗句唯美简短
16 生日婚礼祝福语简短精辟
17 虎年喝酒拜年祝福语简短
18 教师闺蜜祝福语简短