简体   繁体   English

如何确保方法返回值?

[英]How can I ensure that a method returns a value?

The method I have written below is supposed to act on a BinaryTreeNode to flatten the tree 'beneath' that node. 我下面编写的方法应该作用在BinaryTreeNode上,以使该节点“下方”的树变平。 My if-else if-else together with a recursive (?) structure, to my understanding, will always return a value, but I am getting an error in Eclipse saying "This method must return a result of type <Integer> . 据我所知,我的if-else if-else连同递归(?)结构将始终返回一个值,但是我在Eclipse中遇到一条错误,提示“此方法必须返回类型为<Integer>的结果。

After doing research, I believe that Java cannot quite tell that the method will always return a correct value, since the return statement is in an 'else'. 经过研究,我相信Java不能完全确定该方法将始终返回正确的值,因为return语句位于“ else”中。 Is this the problem? 这是问题吗? How can I alternatively design the method to avoid this problem? 我如何可以替代地设计方法来避免此问题?

//doFlatten acts on a BinaryTreeNode and takes a storage list as an argument
//it will call itself on any children on the node
//if the node is a leaf, it will update the storage list and return that updated list
public List<Integer> doFlatten(List<Integer> result){
    if (this.leftChild != null){
        this.leftChild.doFlatten(result);
    }
    else if (this.rightChild != null){
        this.rightChild.doFlatten(result);
    }
    else{
        result.add(this.value); //add leaf to result list
        return result;
    }
}

Make the return type void and remove return result . 使返回类型为void并删除return result There's no need to return the result object, since it's the same result object that was passed in (the caller already has a reference to it). 不需要返回结果对象,因为它与传入的结果对象相同(调用者已经对其进行了引用)。

The best bet is to define the a variable with the return type of the method and initialize it a default value, assign the proper value of the result in the method, and in the end, as the last line of the method, return this variable. 最好的选择是使用方法的返回类型定义一个变量,并将其初始化为默认值,在方法中分配结果的正确值,最后,作为方法的最后一行,返回此变量。

For your method, it may look like this: 对于您的方法,它可能看起来像这样:

public List<Integer> doFlatten(List<Integer> result) {
    List<Integer> realResult = new ArrayList<>(result);
    if (this.leftChild != null){
        this.leftChild.doFlatten(result);
    }
    else if (this.rightChild != null){
        this.rightChild.doFlatten(result);
    }
    else{
        result.add(this.value); //add leaf to result list
        realResult.add(this.value);
        //avoid return statement here
        //return result;
    }
    //single point for return statement
    return realResult;
}

But, for this case, as you can see in code above, it seems meaningless to even return a result because the proper result of this method is stored in List<Integer> result . 但是,对于这种情况,如您在上面的代码中所看到的,即使返回结果似乎也没有意义,因为此方法的正确结果存储在List<Integer> result So, just make your method void : 因此,只需使您的方法void

public void doFlatten(List<Integer> result) {
    //rest of your code...
}

Your method actually doesn't always return a value (the first if and first else) don't have a return. 您的方法实际上并不总是返回值(第一个if和first else)没有返回值。

This seems to be what you want: 这似乎是您想要的:

    public List<Integer> doFlatten(List<Integer> result){
        if (this.leftChild != null){
            this.leftChild.doFlatten(result);
        }
        else if (this.rightChild != null){
            this.rightChild.doFlatten(result);
        }
        else{
            result.add(this.value); //add leaf to result list
        }
        return result;
    }

Examine your code again. 再次检查您的代码。 You have only one return statement in the last else branch. 最后一个else分支中只有一个return语句。 This means that your method returns value only if it arrives to this point. 这意味着您的方法仅在到达此点时才返回值。 This is exactly what compiler reports you. 这正是编译器向您报告的内容。

So, the question "how to ensure that my method returns value" can be answered like "compile your code". 因此,可以像“编译您的代码”那样回答“如何确保我的方法返回值”的问题。 If you managed to compile code be sure that your method indeed returns value or throws exception. 如果您设法编译代码,请确保您的方法确实返回值或引发异常。

If however you are actually asking about the best coding practices that help you to avoid such compilation errors IMHO there is no one 100% correct answer. 但是,如果您实际上是在询问可以帮助您避免此类编译错误的最佳编码实践,恕我直言,则没有一个100%正确的答案。

See the recommendations of Luigi Mendoza. 请参阅Luigi Mendoza的建议。 In some cases they are good. 在某些情况下,它们是好的。 However (sorry, Luigi) I cannot agree that they always good. 但是(对不起,路易吉)我不能同意他们总是很好。 I'd say that you should avoid if/else structures when it is possible. 我要说的是,应尽可能避免使用if / else结构。 For example series of if statements with return in the end of if block in some cases more readable. 例如,在某些情况下,在if块的末尾带有return的一系列if语句更具可读性。

I think you just want : 我想你只想要:

//doFlatten acts on a BinaryTreeNode and takes a storage list as an argument
//it will call itself on any children on the node
//if the node is a leaf, it will update the storage list and return that updated list
public List<Integer> doFlatten(List<Integer> result){
    if (this.leftChild != null){
        return this.leftChild.doFlatten(result);
    }
    else if (this.rightChild != null){
        return this.rightChild.doFlatten(result);
    }
    else{
        result.add(this.value); //add leaf to result list
        return result;
    }
}

Notice that in my version, all predicate outcomes lead to a List<Integer> being returned, where in your case, only the else clause does. 请注意,在我的版本中,所有谓词结果都导致返回List<Integer> ,在您的情况下,只有else子句会返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM