java - 不兼容的类型会返回 statictype 的泛型方法
问题描述
我试图实现一个 BST 类就像
class BST<T> {
private Node<T> root;
private static class Node<T> {
T element;
Node<T> left;
Node<T> right;
}
}
我想在以 Node 作为参数并返回后继节点的方法中获取后继节点
private <T> Node<T> get(Node<T> current node){
Node<T> successor = root;
}
在这里我得到不兼容的类型。如果我删除绑定的方法
private Node<T> get(Node<T> current node) {
Node<T> successor = root;
}
现在它正在编译。是什么原因?
解决方案
reason is T has already been defined at class level BST<T>
. which means that you have to use BST<String>.method();
If you remove <T>
from BST
, the type will then have to be specified at the method level and you will need to add <T>
at your method declaration. And calling the method will be BST.method<String>();
推荐阅读
- vb.net - 使用 iTextSharp 删除或删除创建的表
- python - 如果我有一个测试集 (5000, 784) 但测试标签 (2000,) 如何进行标准化
- audio - 检测音频流(实时)是否包含特定声音
- python - DEBUG = False /“服务器错误(500)” - Django
- python - Python中Break语句之前的代码未执行
- java - 如何解决执行时缺少所需的类:javax/xml/bind/JAXBException
- java - mybatis collection如何保留重复数据?
- haml - 可以将 HAML 文件导入另一个 HAML 文件吗?
- python - 在 MQTT 中发送文件的问题
- python - 如何在 Python 中使用 json 使排行榜显示名称而不是“无”?