java - Java中的继承,无法访问子类函数/方法

标签 java function inheritance binary-tree binary-search-tree

我在访问 bin_stree 对象中的任何公共(public)函数时遇到问题:

src/main.java:

public static void main(String[] args) 
{
        b_node <Integer> root=new b_node <Integer> (10);

    bin_tree <Integer> bt1=new bin_tree<Integer> (root);
        bt1=new bin_stree<Integer>();
        bt1.insert(root,20);//insert doesnt show up as a suggestion in eclipse???


}
src/bin_stree.java: 
public class bin_stree <E extends Comparable <E>> extends bin_tree 
{    
    ...
    public boolean insert(b_node<E> localroot, E target){..}
    ...
}

src/bin_tree.java:
public class bin_tree <E> 
{
     public bin_tree(b_node<E> root)
     {
        this.root=root;
     }
   ...
}

最佳答案

不,不会 - 因为 bt1编译时类型是 bin_tree<Integer> 。 Java 根据表达式的编译时类型解析方法签名等 - 仅根据所涉及对象的实际类型在执行时覆盖它。

你可能应该做 bin_tree具有抽象的抽象类 insert方法,然后在具体子类中实现。或者它可以是一个开始的接口(interface),除非您实际上计划实现某些方法。

(您绝对还应该更改类名称以遵循 Java 命名约定。例如,BinaryTree 对于父类(super class)来说将是一个更易读的名称。)

关于java - Java中的继承,无法访问子类函数/方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16721042/

相关文章:

C# 在继承 get 中使用 base 或 this

java - 更有效地从控制台读取 int 值

java - 如何在android中使用指针创建自定义弹出菜单

java - 从请求的 getParameter 中设置属性值

javascript - 将 $(this) 传递给 jQuery fadeOut 回调

javascript - 访问父类中的变量

java - 修复分支覆盖 Sonar 问题

c++ - 错误 : Control may reach end of non-void function/

function - Coffeescript中的多行函数调用

c++ - 使用纯抽象析构函数从类继承