[英]How to create an Interface for a class that extends JDialog (or in general another class)
I'm developing a Java application that makes use of Swing. 我正在开发一个利用Swing的Java应用程序。 I have a class that extends a JDialog, like this:
我有一个扩展JDialog的类,如下所示:
public class customDialog extends JDialog {
//Custom buttons listeners etc.
}
Now what is the manner (best practice) to create an interface for it? 现在为它创建接口的方式(最佳实践)是什么? I would write:
我会写:
public interface customDialogInterface {
//Custom methods signatures
}
and then use the "implement customDialogInterface" in customDialog but in this manner when I use the customDialogInterface in my code I have no access to the JDialog methods. 然后在customDialog中使用“实现customDialogInterface”,但是以这种方式,当我在代码中使用customDialogInterface时,就无法访问JDialog方法。 Otherwhise JDialog is a class so I can't extend it in my interface.
JDialog是一个类,因此我无法在我的接口中对其进行扩展。 Furthermore I can't extend customDialogInterface with the interfaces that JDialog implements because one of them is package protected.
此外,我无法使用JDialog实现的接口扩展customDialogInterface,因为其中之一是受程序包保护的。
What is the correct manner to proceed in those (I suppose commons) cases? 在那些(我想是公地)案件中,正确的处理方式是什么?
UPDATE 1: I'll try to explain better reformulating my question. 更新1:我将尝试解释更好地重新表达我的问题。 There is a programming principle that tells "program using interfaces and not concrete classes".
有一种编程原理告诉“使用接口而不是具体类的程序”。 Basing on it, how can I create an interface for my CustomDialog?
在此基础上,如何为CustomDialog创建界面? If I do:
如果我做:
public interface CustomDialogInterface {
public void doA();
public void doB();
}
than in the code that uses CustomDialog I have: 比使用CustomDialog的代码中的我有:
CustomDialogInterface myDialog = new CustomDialog();
myDialog.doA(); //OK
myDialog.doB(); //OK
myDialog.setVisible(true); //ERROR
UPDATE 2: I decided to update again my question because basing on the answer it seems that it is not very clear. 更新2:我决定再次更新我的问题,因为基于答案似乎还不太清楚。 It has not many info because it is not yet real code, it is just a theoretical question.
它没有很多信息,因为它不是真正的代码,这只是一个理论问题。 Also if I yet found a possible solution I write here again in another form:
另外,如果我还找到了可能的解决方案,请以另一种形式在这里再次写:
In my application I need a JDialog with my info, fields etc. How can I obtain it? 在我的应用程序中,我需要一个包含我的信息,字段等的JDialog。如何获取它? Defining a class:
定义一个类:
class MyImplementation extends JDialog { public doA(String txt) { //This is a method specific of my implementation } } class MyImplementation扩展JDialog {public doA(String txt){//这是我实现的特定方法}}
Now, I want to follow the rule of good programming "Program by interface, not by implementation" so I need an interface for my custom dialog 现在,我要遵循良好的编程规则:“按界面编程,而不按实现编程”,因此我需要一个用于自定义对话框的界面
interface InterfaceForMyDialog { public doA(String txt); interface InterfaceForMyDialog {public doA(String txt); }
}
Obiously the class MyImplementation has to implement the interface so I change the pseudocode above just adding and implements 令人遗憾的是,类MyImplementation必须实现接口,因此我更改了上面的伪代码,只需添加并实现
class MyImplementation extends JDialog implements InterfaceForMyDialog { public doA(String txt) { //This is a method specific of my implementation } } 类MyImplementation扩展JDialog实现InterfaceForMyDialog {public doA(String txt){//这是我实现的特定方法}}
Now finally I need to use my new dialog somewhere in the external code (let say somewhere in the main method) like this: 现在,最后我需要在外部代码的某处(在main方法的某处)使用新对话框,如下所示:
... InterfaceForMyDialog myDialogInterface = new MyImplementation(...); ... InterfaceForMyDialog myDialogInterface = new MyImplementation(...);
QUESTION: How should you complete the code above to set some text to my custom dialog using the method "doA(String txt)" and then show the dialog itself ? 问题:如何使用“ doA(String txt)”方法完成上面的代码以将一些文本设置到我的自定义对话框中,然后显示对话框本身? I think it is not possible using this structure and I found a solution (see below) using an abstract class.
我认为使用这种结构是不可能的,我找到了一个使用抽象类的解决方案(见下文)。 If I'm wrong or there is a better solution explain please write it here.
如果我错了或者有更好的解决方案,请在这里写下。
You are correct that you can only extend one class; 正确的是,您只能扩展一个班级; since you are extending JDialog, you can implement 0 or more interfaces, but you cannot extend another class.
由于要扩展JDialog,因此可以实现0个或更多接口,但不能扩展另一个类。
If your interface has two methods: 如果您的界面有两种方法:
public interface customDialogInterface
{
public void doA();
public int doB();
}
then your class can have 那你的课就可以
public class customDialog extends JDialog implements customDialogInterface
as long as customDialog
has the methods doA()
and doB()
. 只要
customDialog
具有方法doA()
和doB()
As a note: by convention, both class names and interface names start with capital letters. 注意:按照惯例,类名和接口名都以大写字母开头。 Java programmers the world over will find it easier to understand your code if you use
CustomDialog
and CustomDialogInterface
. 如果您使用
CustomDialog
和CustomDialogInterface
那么全世界的Java程序员都会更容易理解代码。
Ok I found a solution. 好的,我找到了解决方案。 Instead of declaring CustomDialogInterface as interface I declared it as an abstract class that extends the JDialog.
我没有将CustomDialogInterface声明为接口,而是将其声明为扩展JDialog的抽象类。 Then I extend this abstract class with the class that provides the implementation.
然后,我用提供实现的类扩展此抽象类。 If there is a better solution let me know!
如果有更好的解决方案,请告诉我!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.