2015-11-07 105 views
0

我正在为树形数据结构编写几个接口。我得到 Cannot make a static reference to the non-static type E编译错误。无法对静态类型进行静态引用E

这里是我的接口层次:

package com.mohit.dsa.global.position; 

/** 
* @author mohitkum 
*/ 
public interface Position<E> { 
    public E getElement() throws IllegalStateException; 
} 

-

package com.mohit.dsa.tree; 

import com.mohit.dsa.global.position.Position; 

public interface Tree<E> extends Iterable<E> { 
    Position<E> root;//compilation error for E 
} 

这将是巨大的,如果有人能解释这个错误的原因。

回答

2

当你在一个接口中有一个字段时,它是public static final。因此Position root;(即使没有泛型)在接口中无效。进一步假设你正在初始化最终的字段根目录,并说你的树接口实现为:

class A1 implements Tree<A> 

class B1 implements Tree<B> 

这将成为一个问题吧?因为这将转化:

Position<A>根在一种情况下 Position<B>根在另一种情况下

,但你的领域是最后所以这个斜面工作。

以下线程相关: Java - An interface that has static field pointing to class name of its sub class?

相关问题