我在访问 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/