Java泛型难题

Dón*_*nal 4 java generics

这是相关的代码:

public interface Artifact {}    
public interface Bundle implements Artifact {}
public interface Component implements Artifact {}

public interface State<T extends Artifact> {
    void transition(T artifact, State<T> nextState);
}
Run Code Online (Sandbox Code Playgroud)

这允许我定义这个枚举:

enum BundleState implements State<Bundle> {

    A, B, C;

    public void transition(Bundle bundle, State<Bundle> nextState) {}
    }
}
Run Code Online (Sandbox Code Playgroud)

但我想要的方法签名是:

    public void transition(Bundle bundle, BundleState nextState) {}
    }
Run Code Online (Sandbox Code Playgroud)

但这不编译.显然问题在于我如何TState界面中定义,但我无法弄清楚如何解决它.

谢谢,唐

Kir*_*oll 10

事情可能开始变得笨拙,但你可以改变State到:

public interface State<T extends Artifact, U extends State<T, U>> {
    void transition(T artifact, U nextState);
}
Run Code Online (Sandbox Code Playgroud)

并将BundleState更改为:

public enum BundleState implements State<Bundle, BundleState> {
    A, B, C;

    public void transition(Bundle bundle, BundleState nextState) {}
}
Run Code Online (Sandbox Code Playgroud)