我怎么能从另一个类的方法返回一个值

时间:2013-08-12 14:36:55

标签: java

我有一个班级“命题”。在这个课程中,我会用for循环多次调用另一个类的costrucor即“命题”。         “命题”类有一个方法“createProposition”。并且此方法必须将命题的数组列表返回到“命题”,同时将它们保存在“命题集”中。但是我的方法返回null。我也必须使用String方法来改变命题的值。执行命题的数组列表即“propositionsTempArray”应该从“Propositions”类中的“Proposition”类返回到“propList”;

    public class Proposition extends ArrayList<Proposition> {  
        private String productName;
        private Property     property;
        private Serializable value;
        private Type         type;
        private Rating       rating;

        public Proposition() {}


       public ArrayList<Proposition> createPropositions(UserModel userModel,Facts facts,Fact fact){ 
        ...

        assignPropositionVariablesInPreviousProductNoMatch(Fact newfact,String name, String previousProductValue)

       ....
        return propositionsTempArray ;
        }


        private  Proposition assignPropositionVariablesInPreviousProductNoMatch(Fact newfact,String name, String previousProductValue) {
             Proposition proposition=new Proposition();
             proposition.productName=name;
             proposition.type      = Type.PreviousProductNoMatch;
             proposition.value     = previousProductValue;
             proposition.property = (Property) Configuration.typeMapping.getData(("property " . concat((String) newfact.getData("property"))));
             proposition.rating    = (Rating) Configuration.typeMapping.getData(("rating " . concat((String) newfact.getData("rating"))));
             return proposition;
        }

        public String toString() {

             String valueString = "";
                        if (value == null) {
                            valueString = "null";
                        } else {
                            if (value instanceof String[]) {
                                valueString = Arrays.toString((String[]) getValue());
                            } else {
                                valueString = getValue().toString();
                             }

                        return MessageFormat.format(" {0}, {1}, {2}, (Rating:{3},Property:{4})", this.getProductName(), this.getType(),valueString,this.getRating(),this.getProperty());
       }


            public class Propositions extends HashSet<Proposition> implements Comparable<Propositions> {
                public Propositions() {
            }



            public Propositions(UserModel userModel,Facts facts) {



                for (Fact fact: facts) {
                        Proposition propList=new Proposition ();
                        propList.createPropositions(userModel,facts,fact);
                        this.addAll(propList);
                    }
                }

            }

1 个答案:

答案 0 :(得分:1)

        assignPropositionVariablesInPreviousProductNoMatch(Fact newfact,String name, String previousProductValue)

此方法返回一个命题,但你没有抓住结果。

尝试:

Proposition proposition = assignPropositionVariablesInPreviousProductNoMatch(Fact newfact,String name, String previousProductValue);

...

propositionsTempArray.add(proposition);

...

希望这会对你有所帮助。

编辑:我在您的代码中的某处:

propositionsTempArray = new ArrayList<Proposition>();