两类之间可能递归的Java泛型 [英] Possibly recursive Java generics between two classes

查看:81
本文介绍了两类之间可能递归的Java泛型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图制作一些类来控制模型 - 视图 - 演示者应用程序。我已经提出了以下定义,但我正在努力避免递归泛型。

  public abstract class Presenter< V extends View< ; ...>> {

保护V视图;

public Presenter(V view){
this.view = view;
}

// ...
}


public abstract class View< P extends Presenter< ...?> > {

受保护的P主持人;

// ...
}

我想强化两个班级之间的相互关系。我的想法是,我可以为特定的View实例化一个Presenter,这两个类依赖于抽象基类中定义的有用方法,但都知道对象抽象类的子类正在使用中。



我的问题是定义 ..?部分代码。我看不到一种避免递归情况的方法,例如:

  public abstract class View< P extends Presenter< V> ; V延伸View ,Q延伸...> 

即使这个定义并不一致,因为View类现在需要两个通用参数... mass混淆。



我基本上想避免使用引用抽象类类型的类,因此需要在整个具体实现中进行大量的投射,如下所示:

  //简单选项

公共抽象类演示者{

保护视图视图;

public Presenter(View view){
this.view = view;



公共类FooPresenter扩展了Presenter {

public FooPresenter(BarView视图){
super(view);

$ b $ public someMethod(){
((BarView)getView())。viewSpecificMethod();




$ b $ p
$ b $这些类的每个具体实现都需要不断的投射从抽象类型到它知道的类型正在使用中。尝试
$ b $ b

  public abstract class Presenter< V extends View<扩展Presenter<>>> 

  public abstract class View< P extends Presenter<扩展视图<>>>> 

这将限制演示者将任何视图作为其通用参数和视图来拥有任何演示者。 p>

I'm attempting to produce some classes to control a model-view-presenter application. I've come up with the following definitions, but am struggling to avoid recursive generics.

public abstract class Presenter<V extends View<...?>> {

  protected V view;

  public Presenter(V view) {
    this.view = view;
  }

  // ...
}


public abstract class View<P extends Presenter<...?>> {

  protected P presenter;

  // ...
}

I wanted to enforce a mutual relationship between the two classes. The idea being that I could instantiate a Presenter for a particular View, with both classes relying on useful methods defined in the abstract base classes, yet both knowing exactly what subclass of the counterpart abstract class is in use.

My issues is the defining the ..? part of the code. I can't see a way to avoid a recursive situation, such as:

public abstract class View<P extends Presenter<V>, V extends View<Q>, Q extends...>

and even that definition is not consistent, as the View class now takes two generic parameters... mass confusion.

I basically wanted to avoid the classes being littered with references to the abstract class type, necessitating lots of casting throughout the concrete implementations, as below:

// simpler option

public abstract class Presenter {

  protected View view;    

  public Presenter(View view) {
    this.view = view;
  }
}

public class FooPresenter extends Presenter {

  public FooPresenter(BarView view) {
    super(view);
  }

  public someMethod() {
    ((BarView) getView()).viewSpecificMethod();
  }
}

Each concrete implementation of these classes would need to constantly cast from the abstract type to the type it "knows" is in use.

解决方案

Try

public abstract class Presenter<V extends View<? extends Presenter<?>>>

and

public abstract class View<P extends Presenter<? extends View<?>>>

This would restrict presenters to have any view as their generic parameter and views to have any presenter.

这篇关于两类之间可能递归的Java泛型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆