繁体   English   中英

实例化使用 <Foo extends Bar<Foo> &gt;

[英]Instantiating a generic object that uses <Foo extends Bar<Foo>>

我正在使用泛型创建B + Tree类。 这是我的类声明和构造函数的开始:

/**
 * B+ Tree generic type
 *
 * @author Sofian Benaissa
 */
public class BplusTree<K extends Comparable<K>,V>
    {

    private static int maxKeys = 10;
    private Node<K> root;
    private Node<K> currentLeaf;

    /**
     * Default Constructor
     */
    public BplusTree()
    {  
        root = new Node<K>(null, 0);
        currentLeaf = root;
    }

我已经在此处创建了包含该课程完整源代码的要点: https : //gist.github.com/sfyn/7622365

接下来,我尝试在另一个文件中实例化此类。 这行:

private BplusTree<String><String>;

在构造函数中紧接以下行:

bptree = new BplusTree<String><String>();

编译时引发以下错误:

src/FileParser.java:30: error: <identifier> expected
        private BplusTree<String><String> bptree;
                                 ^
src/FileParser.java:30: error: <identifier> expected
        private BplusTree<String><String> bptree;
                                       ^
src/FileParser.java:30: error: <identifier> expected
        private BplusTree<String><String> bptree;
                                               ^
src/FileParser.java:39: error: '(' or '[' expected
                bptree = new BplusTree<String><String>();
                                              ^
src/FileParser.java:39: error: illegal start of expression
                bptree = new BplusTree<String><String>();
                                                       ^
5 errors

语法

private BplusTree<String><String> bptree;

是不正确的。

正确的语法是用逗号分隔的类型参数列表

private BplusTree<String, String> bptree = new BplusTree<String, String>(); /* or new BplusTree<>(); in Java 7 */ 

暂无
暂无

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

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