[英]Why is this error in parameterized constructor?
alright , heres the code : 好吧,继承人的代码:
public class MyClass {
long var;
public void MyClass(long param)
{
var=param; //st1
}
public static void main(string args[])
{
MyClass a,b;
a=new MyClass(); //st2
b=new MyClass(5); //st3
}
}
why error occurs at st3 instead of line st2 ? 为什么错误发生在st3而不是st2行?
This: 这个:
public void MyClass(long param)
declares a method called MyClass
, whereas I suspect you wanted a constructor. 声明一个名为
MyClass
的方法 ,而我怀疑你想要一个构造函数。 Remove the return type: 删除返回类型:
public MyClass(long param)
At that point I suspect you'll get the behaviour you expect. 那时我怀疑你会得到你期望的行为。
Personally I think it's a design flaw that Java allows you to declare a method with the same name as the containing type, but that's a different matter. 我个人认为这是一个设计缺陷,Java 允许您声明一个与包含类型同名的方法,但这是另一回事。
Also note that 另请注意
main(string args[])
should be 应该
main(String args[])
or more conventionally: 或更传统的:
main(String[] args)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.