0

我想在 Java 中重新使用我的自定义对象。我该怎么办。从人们的回答来看,Java 似乎不支持自定义对象。

https://stackoverflow.com/a/11701816/713789

public class FinanceManager {

    public String Compname;

    public FinanceManager(String Compname){
        this.Compname = Compname;
    }

    public static CustomValueReturn(){
        return ;;
        // I need to return a custom object which return too many object as single object.
    }

}

有人请帮助我如何在java中做到这一点。我很少尝试.net,在.net中我制作了结构并返回了一个有很多细节的结构,所以函数可以返回一堆细节而不是单个字符串或对象。

4

4 回答 4

2

尝试:

public static class MyCompound {
   public final String a;
   public final String b;
   public MyCompund(String a,String b) {
     this.a=a;
     this.b=b;
   }
}

public MyCompound testCompundReturn(String input) {
   return new MyCompound(input,"hello");
}
于 2013-04-04T08:42:27.690 回答
1

这很简单。为您的回报创建一个模型类。例如,假设您要返回CustomObject.

class CustomObject {
    private blabla ..;
    /getters setters and what not

}

现在在你的“返回方法”中

public class FinanceManager {
    private CustomObject instanceOfCustomObject;
    public String Compname;

    public FinanceManager(String Compname){
        this.Compname = Compname;
        this.instanceOfCustomObject = new CustomObject();
    }

    public CustomObject getInstanceOfCustomObject(){
        return this.instanceOfCustomObject
        // I need to return a custom object which return too many object as single object.
    }

}
于 2013-04-04T08:42:55.237 回答
0

使用容器、MapList数组。

于 2013-04-04T08:39:44.353 回答
0

如果要返回自定义对象,只需为自定义对象创建一个类并返回该类的实例:

class mycustomobject {
  int int1;
  String string1;
  //and so on...

}

有关更多信息,请参阅教程

如果要返回多个对象,请使用容器或数组。

于 2013-04-04T08:39:46.330 回答