需要参数扩展特定类并实现特定接口 [英] Requiring an argument extends a particular class AND implements a particular interface

查看:189
本文介绍了需要参数扩展特定类并实现特定接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个Java类层次结构,它们共享一个共同的祖先并实现一个通用接口。我需要将指向其中一个的指针传递给另一个类中的方法。

I have two Java class hierarchies that share a common ancestor and implement a common interface. I need to pass a pointer to one of these things to a method in another class.

interface I { ... }

class A extends java.awt.Component implements I { ... }
class B extends java.awt.Component implements I { ... }

class D {
  Component c;
  I i;
  void m(? x) {
    c = (Component) x;
    i = (I) x;
  }
}

有什么我可以替换''用它将允许我传入' A '或' B '?如果我将' x '转换为 java.awt.Component 并将其存储在'中c '和并将其存储在' i '中,我输了强类型的好处。

Is there something I can replace the '?' with that will allow me pass in either an 'A' or a 'B'? If I cast 'x' to a java.awt.Component and store it in 'c' and to an I and store it in 'i', I lose the benefit of strong typing.

我是否需要声明

class D {
  void m(java.awt.Component c, I i) { ... }
}

并使用' m(a,a)'或' m(b,b)'where

and call it with 'm(a, a)' or 'm(b, b)' where

A a = new A();
B b = new B();

我无法创建

abstract class C extends java.awt.Component implements I {...}

并传入,因为 A B 都不是 C

and pass that in because neither A nor B is a C.

BTW,这可以在Scala中完成吗?

BTW, can this be done in Scala?

编辑:我想解决的实际问题是我有两个类,一个扩展 JInternalFrame ,另一个扩展 JPanel 。两者都是抽象的,并为其中显示的小部件提供了一些通用功能(用户可以编辑行,删除行等的JTable)。无论显示的基础对象类型如何,编辑和删除行的代码始终相同。我有几种方法允许用户单击一行,从弹出菜单中选择删除,并在请求确认后,删除所选的行和数据库对象,例如。有时我需要一个框架子组件,有时我需要一个面板子组件。我已经为该委托类型的每个抽象类中的公共功能和一个实例变量创建了一个委托类。 JInternalFrame JPanel 派生类然后只是将实际的实现推迟到委托。反过来,委托类需要一个指向owner类的指针,用于获取所选表行等的回调,以及指向 Component 性质的指针 JOptionPane 确认对话框的每个父级。

The actual problem that I am trying to solve is that I have two classes, one that extends JInternalFrame and another that extends JPanel. Both are abstract and provide some common functionality for widgets displayed in them (JTables where the user can edit rows, delete rows, etc). The code for editing and deleting rows is always the same, regardless of the underlying object types being displayed. I have several methods that allow the user to click a row, select 'Delete' from a popup menu, and, after asking for confirmation, deletes the selected row and database object, for example. Sometimes I need a frame subcomponent and at other times a panel subcomponent. I have created a delegate class for the common functionality and an instance variable in each of the abstract classes of that delegate type. The JInternalFrame and JPanel derived classes then just defer the actual implementations to the delegate. The delegate class, in turn, needs a pointer to the "owner" class for callbacks to get the selected table row, etc., and a pointer to the "Component" nature of each parent for the JOptionPane confirmation dialogs.

使用Java泛型方法非常有效。委托类现在被定义为上的泛型类< T extends Component& DelegateContainer 并且每个所有者抽象类都实现 DelegateContainer (其中声明了回调方法)。

Using the Java generics approach has worked very well. The delegate class is now defined as a generic class on <T extends Component & DelegateContainer and each of the owner abstract classes implements DelegateContainer (where the callback methods are declared).

如果我要在Scala中重写它,我可能会使用traits而不是创建委托类。例如,特征可以将删除功能添加到 JInternalFrame 派生的具体类。

If I were going to rewrite this in Scala, I would probably do something with traits instead of creating a delegate class. The traits could "add" the delete functionality to the JInternalFrame derived concrete class, for example.

感谢提示回复。

推荐答案

拯救的泛型!

public class Test {
    public static void main(String... args){
        new D().m(new A());
        new D().m(new B());
    }
}

interface I {  }

class A extends java.awt.Component implements I {}
class B extends java.awt.Component implements I {}

class D {
  Component c;
  I i;
  <T extends java.awt.Component & I> void m(T x) {
    c = x;
    i = x;
  }
}

这不是最好的做事方式,但在你的情况是有效的。你应该把你的方法分成两个,一个用于 I 行为,另一个用于组件行为。

It's not really the best way to do things but in your case it works. You should really split your method in two, one for the I behavior and another for the Component behavior.

这篇关于需要参数扩展特定类并实现特定接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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