简体   繁体   English

如何为扩展JDialog的类(或通常的其他类)创建接口

[英]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: 另外,如果我还找到了可能的解决方案,请以另一种形式在这里再次写:

  1. 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){//这是我实现的特定方法}}

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

  3. 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){//这是我实现的特定方法}}

  4. 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 . 如果您使用CustomDialogCustomDialogInterface那么全世界的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.

相关问题 如何创建可扩展类并实现接口的通用Java类? - how to create generic java class that extends class and implements interface? 如何从扩展JDialog的另一个类中调用已存在的活动框架,然后删除其所有组件? - how to call active frame that already exist from another class that extends JDialog then remove all of its component? 类实现扩展另一个接口的接口 - Class implements Interface that extends another Interface 如何创建具有扩展另一个类并实现接口的类型参数的基适配器? - How to create base adapter which has type parameter that extends another class and implements an interface? 如何验证泛型在Java中扩展了另一个类/接口? - How to verify a generic type extends another class/interface in Java? 如何从可扩展的类创建实例并实现通用接口? - how to create an instance from a class that extends comparable and implement generic interface? 如何检查一个类是否扩展了另一个类 - How to Check if a class extends another 如何外部化扩展另一个的类 - How to externalize a class that extends another 创建适合接口和一般父类的通用方法 - Create general method to fit interface and general parent class 如何使用由 class 实现的接口方法,它还扩展了另一个 class? - How to use interface method implemented by a class which also extends another class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM