创建返回对象的方法的语法问题(java)
我正在尝试创建一个方法,对两个 timeO 对象求和并返回一个名为 sum 的新 TimeO 对象。 这是相关的代码片段:
public static TimeO add (TimeO t1, TimeO t2)
{
TimeO sum = new TimeO ;
...
}
当我尝试编译它时,我收到此错误消息:
TimeO.java:15: '(' or '[' expected
TimeO sum = new TimeO ;
^
1 error
我想不出为什么它希望我在此处打开一组括号或括号,但我可能不这样做不太明白语法。这里出了什么问题?
I'm trying to create a method that will sum two timeO objects and return a new TimeO object called sum.
Here is the relevant code snippet:
public static TimeO add (TimeO t1, TimeO t2)
{
TimeO sum = new TimeO ;
...
}
When I try to compile it I get this error message:
TimeO.java:15: '(' or '[' expected
TimeO sum = new TimeO ;
^
1 error
I can't think of any reason why it would want me to open a set of parenthasies or brackets here but it's possible that I don't quite understand the syntax. What's going wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
调用构造函数的语法是:
因此,如果您想调用无参数构造函数,您应该使用:
将构造函数调用(这是创建新对象的方式)视为一种特殊的方法调用。
The syntax for calling a constructor is:
So if you want to call a parameterless constructor, you should use:
Think of a constructor call (which is the way you create a new object) as being like a special kind of method call.