Java继承和递归 [英] Java inheritance and recursion

查看:58
本文介绍了Java继承和递归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个超类和一个子类,如下所示:

I have a superclass and a subclass as follows:

class Tree{
 ..
 public void add(..){
 //makes a call to protected function add(..) 
 }//for client to use.
 protected TreeNode add(..){}//recursive function which calls itslef
}

class Stree extends Tree{
 //overrides the recursive add function from class Tree
 protected TreeNode add(..){
   ..
   super.add();//calls the non-recursive add function in superclass.
 }
}

这里的问题是,当我从子类中的新add函数调用super.add()时,它转到了Tree.add().在Tree.add()内部.有一个对add()的调用,该调用在子类中而不是在super中调用递归add函数,即Stree.add()而不是Tree.add(),从而导致无限循环.你们看到问题出在哪里吗?

The problem here is that when I call super.add() from the new add function in the subclass, it goes to Tree.add(). Inside Tree.add(). there is a call to add(), which calls the recursive add function in the subclass instead of super, i.e. Stree.add(), instead of Tree.add() which results in an infinite loop. Do you guys see where the problem is?

这是一项家庭作业,因此我无法更改递归函数的名称.我明确要求在不重写任何现有代码的情况下向递归添加函数添加功能,这基本上意味着我将不得不调用原始的add()函数.

This is a homework assignment hence I cannot change the name of the recursive function. I am explicitly required to add functionality to the recursive add function, without rewriting any existing code, which basically means I will have to make a call to the original add() function.

edit:Tree.add()//递归代码.请注意,我无法修改此代码来获得所需的功能.

edit: Code for the Tree.add()//recursive. Note that I cannot modify this code to gain the functionality I seek.

protected StreeNode add(StreeNode node, String value) {
        if (node == null) {
            node = new StreeNode(value);
            numElements++;
        } else if (node.data.compareTo(value) == 0) {
            // do nothing, String was already in Set
        } else if (node.data.compareTo(value) > 0) {
            node.left = add(node.left, value);      // x = change(x)
        } else {
            node.right = add(node.right, value);    // x = change(x)
        }

        return node;
    }

现在我看到这是预期的行为,我将如何实现以下目标:

edit: Now that I see that this is the expected behaviour, how do I go about achieving the following:

  1. 使用原始递归add()
  2. 添加值
  3. 实现额外的功能
  1. Add the value using the original recursive add()
  2. Implement extra functionality

推荐答案

在没有看到参数的情况下,我认为void add(...)是向树中添加内容的方法,而受保护的递归方法则寻找要添加到其中的节点,然后执行添加.

Without seeing the parameters I assume void add(...) is the method to add something into the tree, whereas the protected recursive method looks for the node to add to and then performs the add.

我进一步假设公共非递归方法将树的根作为起始参数传递给递归方法,而递归方法要么传递左孩子,要么传递右孩子,直到您碰到叶子.因此,调用非递归方法可能会一次又一次地从根目录开始.

I further assume the public non-recursive method passes the tree's root to the recursive method as a start parameter while the recursive method either passes the left or right child until you hit a leaf. Calling the non-recursive method might thus in starting at the root again and again.

因此,我想说递归和继承方法不应调用非递归版本,而应再次调用自身.

Thus I'd say that the recursive and inherited method should not call the non-recursive version but should call itself again.

这篇关于Java继承和递归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆