Java 能否将对象强制转换为实现接口的对象?(爪哇)

Java 能否将对象强制转换为实现接口的对象?(爪哇),java,user-interface,casting,object,Java,User Interface,Casting,Object,能否将对象强制转换为实现接口的对象?现在,我正在构建一个GUI,我不想一次又一次地重写确认/取消代码(一个确认弹出窗口) 所以,我要做的是编写一个类,该类通过它所使用的类,并告诉该类用户是否按了确认或取消。该类始终实现某个接口 代码: 我意识到我可能过于复杂化了,但现在我遇到了这个问题,我真的想知道是否可以将一个对象强制转换为实现某个接口的另一个对象。当然可以-强制转换的常用语法适用。您甚至可以将参数类型声明为接口,而不是对象 class ConfirmFrame extends JFrame

能否将对象强制转换为实现接口的对象?现在,我正在构建一个GUI,我不想一次又一次地重写确认/取消代码(一个确认弹出窗口)

所以,我要做的是编写一个类,该类通过它所使用的类,并告诉该类用户是否按了确认或取消。该类始终实现某个接口

代码:


我意识到我可能过于复杂化了,但现在我遇到了这个问题,我真的想知道是否可以将一个对象强制转换为实现某个接口的另一个对象。

当然可以-强制转换的常用语法适用。您甚至可以将参数类型声明为接口,而不是
对象

class ConfirmFrame extends JFrame implements ActionListener
{
    JButton confirm = new JButton("Confirm");
    JButton cancel = new JButton("Cancel");
    ActionListener a;

    public ConfirmFrame(ActionListener a)
    {
        // Irrelevant code here
        add(confirm);
        add(cancel);
        this.a = a;
    }

    public void actionPerformed( ActionEvent evt)
    {
        a.actionPerformed(evt);
    }
}
编辑:当然,变量的类型也需要是接口,或者,您可以在对其调用方法时强制转换它,即

ActionPerformer o;
...
this.o = (ActionPerformer)o;


当然可以-通常的强制转换语法适用。您甚至可以将参数类型声明为接口,而不是
对象

class ConfirmFrame extends JFrame implements ActionListener
{
    JButton confirm = new JButton("Confirm");
    JButton cancel = new JButton("Cancel");
    ActionListener a;

    public ConfirmFrame(ActionListener a)
    {
        // Irrelevant code here
        add(confirm);
        add(cancel);
        this.a = a;
    }

    public void actionPerformed( ActionEvent evt)
    {
        a.actionPerformed(evt);
    }
}
编辑:当然,变量的类型也需要是接口,或者,您可以在对其调用方法时强制转换它,即

ActionPerformer o;
...
this.o = (ActionPerformer)o;


可以向上或向下类型层次结构投射对象;有时这是安全的,有时不是。如果您试图将变量强制转换为不兼容的类型(即试图说服编译器它不是),您将得到一个运行时异常(即错误)。使用更通用的类型(如将
ActionListener
更改为
对象
)称为向上转换,并且总是安全的,假设您要转换的类是当前类的祖先之一(并且
对象
是Java中所有事物的祖先)。只有当对象实际上是更具体类型的实例时,才能转到更具体的类型(如从
ActionListener
转换到
MySpecialActionListener

因此,在您的例子中,听起来您试图做的是说ConfirmFrame实现了接口ActionListener。我假设该接口包括:

public void actionPerformed( ActionEvent evt);
然后,在这个虚拟方法的实现中,您希望将evt委托给传递给构造函数的任何对象
o
。这里的问题是
对象
没有名为
actionPerformed
的方法;只有一个更专业的类(在本例中,是
ActionListener
的实现,比如
ConfirmFrame
类)才会拥有它。因此,您可能希望该构造函数采用
ActionListener
而不是
对象

class ConfirmFrame extends JFrame implements ActionListener
{
    JButton confirm = new JButton("Confirm");
    JButton cancel = new JButton("Cancel");
    ActionListener a;

    public ConfirmFrame(ActionListener a)
    {
        // Irrelevant code here
        add(confirm);
        add(cancel);
        this.a = a;
    }

    public void actionPerformed( ActionEvent evt)
    {
        a.actionPerformed(evt);
    }
}

当然,比“o”或“a”更具解释性的变量名可能会帮助您(以及阅读本文的任何其他人)理解为什么要将ActionListener传递给另一个ActionListener。

您可以在类型层次结构上下转换对象;有时这是安全的,有时不是。如果您试图将变量强制转换为不兼容的类型(即试图说服编译器它不是),您将得到一个运行时异常(即错误)。使用更通用的类型(如将
ActionListener
更改为
对象
)称为向上转换,并且总是安全的,假设您要转换的类是当前类的祖先之一(并且
对象
是Java中所有事物的祖先)。只有当对象实际上是更具体类型的实例时,才能转到更具体的类型(如从
ActionListener
转换到
MySpecialActionListener

因此,在您的例子中,听起来您试图做的是说ConfirmFrame实现了接口ActionListener。我假设该接口包括:

public void actionPerformed( ActionEvent evt);
然后,在这个虚拟方法的实现中,您希望将evt委托给传递给构造函数的任何对象
o
。这里的问题是
对象
没有名为
actionPerformed
的方法;只有一个更专业的类(在本例中,是
ActionListener
的实现,比如
ConfirmFrame
类)才会拥有它。因此,您可能希望该构造函数采用
ActionListener
而不是
对象

class ConfirmFrame extends JFrame implements ActionListener
{
    JButton confirm = new JButton("Confirm");
    JButton cancel = new JButton("Cancel");
    ActionListener a;

    public ConfirmFrame(ActionListener a)
    {
        // Irrelevant code here
        add(confirm);
        add(cancel);
        this.a = a;
    }

    public void actionPerformed( ActionEvent evt)
    {
        a.actionPerformed(evt);
    }
}

当然,比“o”或“a”更具解释性的变量名可能会帮助您(以及其他阅读本文的人)理解为什么要将ActionListener传递给另一个ActionListener。

上述答案的示例用法:)


上述答案的示例用法:)


那么,呃,你会说:“this.o=(ActionListener)o;”?我之前试过这么做,但是找不到actionPerformed()方法。哦,等等,修复了。它找不到它的原因是b/c i将对象初始化为对象而不是接口。谢谢,Evgeny:DSo,呃,你会说:“this.o=(ActionListener)o;”?我之前试过这么做,但是找不到actionPerformed()方法。哦,等等,修复了。它找不到它的原因是b/c i将对象初始化为对象而不是接口。谢谢,Evgeny:D您应该使用所有类实现的接口而不是对象,或者更好地使用泛型而不是对象。您应该使用所有类实现的接口而不是对象,或者更好地使用泛型而不是对象。感谢详细的解释:D谢谢详细的解释:D谢谢详细的解释:D