【问题标题】:Cannot make a static reference to the non-static type E无法对非静态类型 E 进行静态引用
【发布时间】:2015-11-07 06:16:00
【问题描述】:

我正在为树数据结构编写几个接口。我正进入(状态 Cannot make a static reference to the non-static type 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
}

如果有人能解释这个错误的原因,那就太好了。

【问题讨论】:

    标签: java generics interface


    【解决方案1】:

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

    class A1 implements Tree<A>
    
    class B1 implements Tree<B>
    

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

    Position&lt;A&gt;root 在一种情况下 Position&lt;B&gt;root 在另一种情况下

    但你的领域是最终的,所以这不能工作。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-15
      • 2015-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-25
      相关资源
      最近更新 更多