hhh*_*hhh 16 java constructor return-value
$ cat Const.java
public class Const {
String Const(String hello) {
return hello;
}
public static void main(String[] args) {
System.out.println(new Const("Hello!"));
}
}
$ javac Const.java
Const.java:7: cannot find symbol
symbol : constructor Const(java.lang.String)
location: class Const
System.out.println(new Const("Hello!"));
^
1 error
Run Code Online (Sandbox Code Playgroud)
Ash*_*Ash 25
您定义的实际上不是构造函数,而是一个名为的方法Const.如果您将代码更改为此类代码,它将起作用:
Const c = new Const();
System.out.println( c.Const( "Hello!" ) );
Run Code Online (Sandbox Code Playgroud)
如果未显式定义特定构造函数,则编译器会自动创建无参数构造函数.
实际上,java类中的构造函数不能返回必须采用以下形式的值
public class Test {
public Test(/*here the params*/) {
//this is a constructor
//just make some operations when you want to create an object of this class
}
}
Run Code Online (Sandbox Code Playgroud)
检查这些链接 http://leepoint.net/notes-java/oop/constructors/constructor.html http://java.sun.com/docs/books/tutorial/java/javaOO/constructors.html
| 归档时间: |
|
| 查看次数: |
78535 次 |
| 最近记录: |