Java:构造函数如何返回值?
构造函数无法返回值。那是最后的决定。它的含义是一样的——它不能有返回类型,这就是为什么会出现编译错误。您可以说返回值总是隐式地由构造函数创建的对象。实际上,java类中的构造函数不能返回值,它必须是以下形式Java:构造函数如何返回值?,java,constructor,return-value,Java,Constructor,Return Value,构造函数无法返回值。那是最后的决定。它的含义是一样的——它不能有返回类型,这就是为什么会出现编译错误。您可以说返回值总是隐式地由构造函数创建的对象。实际上,java类中的构造函数不能返回值,它必须是以下形式 $ cat Const.java public class Const { String Const(String hello) { return hello; } public static void main(String[] args) { S
$ 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
Const c = new Const();
System.out.println( c.Const( "Hello!" ) );
检查这些链接
构造函数不能像“普通”函数那样具有返回值。在创建相关类的距离时调用它。
它用于执行该实例的初始化。构造函数不能返回值;可以说,它们返回构造的对象 出现错误是因为编译器正在查找以字符串作为参数的构造函数。由于您没有声明构造函数,因此唯一可用的构造函数是不带任何参数的默认构造函数 为什么我说你没有声明构造函数?因为一旦你为你的方法声明了一个返回值/类型,它就不再是一个构造函数,而是一个常规方法 从: 类包含的构造函数是 调用以从 班级蓝图。建造师 声明看起来像方法 声明,除非它们使用 类的名称,并且没有返回 类型
如果你详细说明你想要达到的目标,有人可能会告诉你如何达到这个目标。你所定义的实际上不是一个构造函数,而是一个名为
Const
的方法。如果您将代码更改为类似的内容,它将起作用:
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
}
}
如果没有明确定义特定的构造函数,编译器会自动创建一个无参数构造函数。我认为产生所需效果的最佳方法是:
Const c = new Const();
System.out.println( c.Const( "Hello!" ) );
public class Const {
private String myVar;
public Const(String s) {
myVar = s;
}
public String getMyString()
{
return myVar;
}
public static void main(String[] args) {
Const myConst = new Const("MyStringHere");
System.out.println(myConst.getMyString());
}
}
这将替换以前使用的
public String Const()
方法,并通过重写Object
的public String toString()
方法(所有Java类都从该方法继承),在打印对象时,对象的字符串值将正确打印,因此主方法保持不变 构造函数不能返回值,因为构造函数隐式返回对象的引用ID,并且构造函数也是方法,方法不能返回多个值。所以我们明确地说构造函数没有返回值。已经有很多很好的答案了。我只想补充一点,如果您想通过调用构造函数来获得与对象本身分离的返回代码,您可以将构造函数包装在工厂方法中,该方法在创建时,例如,可以在构造的对象内执行一些数据验证,并根据结果返回一个布尔值。要从构造函数传回值,只需将数组作为参数传入即可。
为了说明这一原则:
public class Const {
private String str;
public Const(String hello) {
str = hello;
}
public String toString() {
return str;
}
public static void main(String[] args) {
System.out.println(new Const("Hello!"));
}
}
}考虑将这个问题的标题编辑为“Java:我的构造函数在这里返回值吗?”我有一个关于Java如何从构造函数返回值的特定问题,我认为这个问题是关于更一般的主题。。。相反,它类似于“帮助,我不能给我的构造函数赋值!”您应该创建一个toString()函数,而不是getMyString()。所以您可以执行System.out.println(myConst)
/************************************************
Put the return value as a private data member, which gets
set in the constructor. You will need a public getter to
retrieve the return value post construction
******************************************************/
class MyClass
{
private Boolean boolReturnVal;
public Boolean GetReturnVal() { return(boolReturnVal); }
MyClass() // constructor with possibly args
{
//sets the return value boolReturnVal
}
public static void main(String args[])
{
MyClass myClass = new MyClass();
if (myClass.GetReturnVal())
{
//success
}
}