JAVA把对象转成接口,你能将一个对象转换为实现接口的对象吗? (JAVA)

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值