你能将一个对象转换为实现接口的对象吗? (JAVA) [英] Can you cast an object to one that implements an interface? (JAVA)

查看:377
本文介绍了你能将一个对象转换为实现接口的对象吗? (JAVA)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你可以将对象转换为实现接口的对象吗?现在,我正在构建一个GUI,我不想一遍又一遍地重写确认/取消代码(确认弹出)。



所以,我想做的是编写一个类,它会传递它使用的类,并告诉该用户是否按下Confirm或Cancel。

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

public ConfirmFrame(Object o)
{
//此处不相关的代码
add(confirm);
add(cancel);
this.o =(/ *这里是什么?* /)o;
}

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

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

解决方案

您可以在类型层次结构中向上或向下投射对象;有时这是安全的,有时它不是。如果你试图将一个变量转换为不兼容的类型(即试图说服编译器它是不是),你会得到一个运行时异常(即错误)。转到更一般的类型(例如将 ActionListener 更改为 Object )称为向上转换,假设你转换的类是你当前类的祖先之一(和 Object 是Java中所有东西的祖先)。转到更具体的类型(例如从 ActionListener MySpecialActionListener 的转换)仅在您的对象实际 更具体类型的实例。



所以,在你的情况下,它听起来像你想做的是说ConfirmFrame实现接口ActionListener。我假设该界面包括:

  public void actionPerformed(ActionEvent evt); 

然后在这个虚拟方法的实现中,你想将evt委托给任何对象 o 已传入构造函数。这里的问题是 Object 没有一个名为 actionPerformed 的方法;只有一个更专门的类(在这种情况下, ActionListener 的实现像你的 ConfirmFrame 类)所以你可能想要的是该构造函数使用 ActionListener 而不是 Object



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

public ConfirmFrame(ActionListener a)
{
//这里不相关的代码
add(confirm);
add(cancel);
this.a = a;
}

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

当然,或a可能会帮助你(和任何人读这个)理解你为什么你传递一个ActionListener到另一个ActionListener。


Can you cast an object to one that implements an interface? Right now, I'm building a GUI, and I don't want to rewrite the Confirm/Cancel code (A confirmation pop-up) over and over again.

So, what I'm trying to do is write a class that gets passed the class it's used in and tells the class whether or not the user pressed Confirm or Cancel. The class always implements a certain interface.

Code:

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

    public ConfirmFrame(Object o)
    {
        // Irrelevant code here
        add(confirm);
        add(cancel);
        this.o = (/*What goes here?*/)o;
    }

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

I realize that I'm probably over-complicating things, but now that I've run across this, I really want to know if you can cast an object to another object that implements a certain interface.

解决方案

You can cast objects either up or down the type hierarchy; sometimes this is safe, and sometimes it isn't. If you attempt to cast a variable to an incompatible type (i.e. try to convince the compiler it's something it's not), you'll get a runtime exception (i.e. error). Going to a more general type (like changing an ActionListener into an Object) is called upcasting, and is always safe, assuming the class you're casting to is one of the ancestors of your current class (and Object is an ancestor of everything in Java). Going to a more specific type (like casting from ActionListener to MySpecialActionListener) only works if your object actually is an instance of the more specific type.

So, in your case, it sounds like what you're trying to do is say that ConfirmFrame implements the interface ActionListener. I assume that that interface includes:

public void actionPerformed( ActionEvent evt);

And then here, in your implementation of that virtual method, you want to delegate the evt to whatever object o was passed into the constructor. The problem here is that Object doesn't have a method called actionPerformed; only a more specialized class (in this case, an implementation of ActionListener like your ConfirmFrame class) would have it. So what you probably want is for that constructor to take an ActionListener instead of an Object.

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);
    }
}

Of course, more explanatory variable names than "o" or "a" would probably help you (and anyone else reading this) understand why you're passing an ActionListener into another ActionListener.

这篇关于你能将一个对象转换为实现接口的对象吗? (JAVA)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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