在Java中的同一个接口中实现多个类? [英] Implement multiple classes in the same interface in Java?

查看:1597
本文介绍了在Java中的同一个接口中实现多个类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个类,每个类在每个类中实现多个不同的方法。现在问题陈述是我希望在另一个类文件中使用所有这些方法(可能大约200个这样的不同类文件/方法),这些方法来自上面的类文件。

I have got multiple classes which each implement multiple different methods within each. Now the problem statement is that I wish to use the methods from all these (maybe around ~200 such different class files/methods) in another class file which all different methods from the above class files.

我认为如果我实现了一个列出了所有这些不同方法的接口,那么我只是调用/ import / reference那个单一接口并且可以使用所有方法吗?但是我被卡住了,因为这个解决方案似乎不起作用。

I thought that if I implement an interface which has all these various methods listed, then I just call/import/reference that single interface and can use all the methods? But I am stuck, as this solution does not seem to work.

与上述相反的工作(即单个类实现2个接口: http://tutorials.jenkov.com/java/interfaces.html )。希望检查单个接口是否可以使用多个类,而没有声明在接口内引用的每个类中的所有方法的开销?

The opposite of the above works (i.e. single class implements 2 interfaces: http://tutorials.jenkov.com/java/interfaces.html). Wish to check if the single interface can use multiple classes, without the overhead of declaring all the methods in each class that is being referenced inside the Interface?

As一个例子:有没有什么方法可以在同一个界面中实现2个不同的类,而每个类都有自己的抽象类?好像这个类是抽象的,那么我无法在下面的例子Application类中使用它的方法:

As an example: Is there any way in which I can implement 2 different classes in the same interface, without each having the abstract class for each? As if the class is abstract, then I am unable to use the methods from it in the below example "Application" class:

Common commonClass = new ABC_FamilyGivenName();

如果ABC_FamilyGivenName类是抽象类,则不允许上述情况。

The above is not allowed, if the ABC_FamilyGivenName class is an abstract class.

INTERFACE:

public interface Common {
    void ABC_GivenNames();
    void ABC_FamilyNames();
    void ABC_Gender();
    void ABC_BirthDay();
}



IMPLEMENTATION CLASSES:

public class ABC_FamilyGivenName extends Base implements Common {

    public void ABC_GivenNames(){
        // Implementation code
    }

    public void ABC_FamilyNames(){
        // Implementation code
    }
}

public class ABC_DOBGender extends Base implements Common {

    public void ABC_Gender(){
        // Implementation code
    }

    public void ABC_BirthDay(){
        // Implementation code
    }
}



USE IMPLEMENTED CLASS:
public class Application extends Base {

    Common commonClass = new ABC_FamilyGivenName();
    /* DO I NEED THIS? I THINK I DO, BUT CODE/JAVA SAYS I DO NOT
     * Common commonClass = new ABC_DOBGender();
     */

    public void ELP_C0050_PassportDetails(){
        commonClass.ABC_GivenNames();
        commonClass.ABC_FamilyNames();
        commonClass.ABC_DOB();
        commonClass.ABC_Gender();
    }
}

我有2个名为ABC_FamilyGivenName& ABC_DOBGender。
我创建了一个Common公共接口。

I have 2 classes called ABC_FamilyGivenName & ABC_DOBGender. I have created an interface Common.

我想在另一个名为Application的类中使用上述两个类中的方法。

I want to use the methods in both the above classes in another class called Application.

使用当前的实现,Java希望我向ABC_FamilyGivenName和&添加@Override。 ABC_DOBGender:

With the current implementation, Java wants me to add an @Override to both the ABC_FamilyGivenName & ABC_DOBGender:

IMPLEMENTATION CLASSES:

public class ABC_FamilyGivenName extends Base implements Common {

    public void ABC_GivenNames(){
        // Implementation code
    }

    public void ABC_FamilyNames(){
        // Implementation code
    }

    @Override
    public void ABC_BirthDay() {}

    @Override
    public void ABC_Gender() {} 
}

public class ABC_DOBGender extends Base implements Common {

    public void ABC_Gender(){
        // Implementation code
    }

    public void ABC_BirthDay(){
        // Implementation code
    }

    @Override
    public void ABC_GivenName() { }     

    @Override
    public void ABC_FamilyName() { }        
}

我可以避免上述@Override和只使用第一个例子中给出的没有这些的类?

Can I avoid the above @Override and just use the classes without these as given in the first example?

推荐答案

要实现以下代码更改,请确保使用java8

To implement the below code change please make sure you use java8

public interface Common {

 default public void ABC_GivenNames() {
 }

 default public void ABC_FamilyNames() {
 }

 default public void ABC_Gender() {
 }

 default public void ABC_BirthDay() {
 }

}

实施类别:

public class ABC_FamilyGivenName extends Base implements Common {

    public void ABC_GivenNames(){
        // Implementation code
    }

    public void ABC_FamilyNames(){
        // Implementation code
    }

}

public class ABC_DOBGender extends Base implements Common {

    public void ABC_Gender(){
        // Implementation code
    }

    public void ABC_BirthDay(){
        // Implementation code
    }

}

这篇关于在Java中的同一个接口中实现多个类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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