【发布时间】:2014-05-18 06:10:22
【问题描述】:
以下代码不起作用,因为编译器说函数 AnotherNewClass() 不存在?这是为什么?构造函数不只是一个函数吗?为什么构造函数不能不引用特定对象?
class AnotherNewClass
{
public AnotherNewClass(){
System.out.println("Hello World!!");
}
public AnotherNewClass(String arg){
System.out.println("Hello World!!");
}
public static void main(String []args){
AnotherNewClass("Hello World!!");//This is the offending code; where the compiler throws an error
}
}
PS。从下面的几个 cmets 中我想澄清一下,我知道我没有使用 new 关键字,这个问题的目的是强调函数和构造函数之间的区别(不能在没有'new'的情况下调用)
【问题讨论】:
-
应该是
new AnotherNewClass("Hello World!!"); -
你永远不会显示有问题的代码......嗯。
标签: java function constructor