实现一个引用多个类的Java类/接口 [英] Implement a single Java class/interface, which references multiple classes

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

问题描述

问题陈述
我们有多个类文件(~200 +),每个文件都实现不同的方法,有些也使用彼此的方法来实现自己的方法。

Problem Statement: We have multiple class files (~200+), which each implement different methods and some use the methods from each other also to implement their own methods.

我们希望在代码中调用多个这些类方法(~20-30),并让它们提供写入UI网页和从网页读取等功能。

We wish to call multiple of these class methods (~20-30) in our code and have them deliver functionality like writing to a UI Web page and reading from a web page.

由于我们希望简化这个过程,我们需要一个包含所有这些多类引用(~200 +)的类/接口,我们只需要导入/在我们的代码中包含此单个类/接口,然后使用它来引用类文件中的所有方法。

As we wish to make this process simple, we want a single class/interface which contains all these multiple class references (~200+), and we just need to import/include this single class/interface in our code and then using it to reference all the methods in the class files.

问题
以上可能吗?如果是这样,如何 - 通过使用类或接口或其他东西?

Question: Is the above possible? If so, how - by using a class or interface or something else?

我提供了我们在这里尝试过的示例和信息:在Java的同一界面中实现多个类?

I have provided examples and information of what we have tried here: Implement multiple classes in the same interface in Java?

基本上,我们已经尝试了

Basically, we have tried


  1. 创建一个其中声明了所有其他类的类

  2. 创建一个接口,它具有多个类的实现(上面提到的~200 +)

  3. 实现了一个类和一个接口

似乎没有办法。

作为我们想要的代码的要点,下面提供了一个例子。

As a gist of what we want in code, have provided an example below.

示例 这不是正确的代码或实现,只是想提供一个示例我们想要在代码中做什么):

我有2个名为 ABC_FamilyGivenName 的类& ABC_DOBGender
我创建了一个界面 Common

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

我想在我的代码中使用上述两个类中的方法class Application

I want to use the methods in both the above classes in my code in class "Application".

使用当前的实现,Java希望我向 ABC_FamilyGivenName & ABC_DOBGender 如果我们有~500个这样的方法,这又会产生开销,因此需要在每个c~200类文件中覆盖所有~500 ??

With the current implementation, Java wants me to add an @Override to both the ABC_FamilyGivenName & ABC_DOBGender. This again creates an overhead, if we have ~500 such methods, and will thus need to override all ~500 in each of the c~200 class files??

***INTERFACE***:

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



***IMPLEMENTATION CLASSES***:

**ABC_FamilyGivenName**
public class ABC_FamilyGivenName extends Base implements Common {

    public void ABC_GivenNames(){
        // Implementation code
    }

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

**ABC_DOBGender**
public class ABC_DOBGender extends Base implements Common {

    public void ABC_Gender(){
        // Implementation code
    }

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



**USE IMPLEMENTED CLASS IN CODE**:
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();
    }
}


推荐答案

所以你可以做更多的事情,但这是我所谈论的一个非常简单的例子。不要在它们上面插入接口,而是将它们插入。

So there is a lot more you can do with this, but this is a very simple example of what I was talking about. Instead of having an interface over them, plug them in.

//Component Classes

public class DOBGender {//Has the specific methods you want

}



public class FamilyGivenName {//Has the specific methods you want

}

//Then just plug them in to this class

public class Person {

    DOBGender dobGender;
    FamilyGivenName fgn;

    public Person(DOBGender dobGender, FamilyGivenName fgn) {//Plug them in
        this.dobGender = dobGender;
        this.fgn = fgn;
    }

    public DOBGender getDobGender() {
        return dobGender;
    }

    public void setDobGender(DOBGender dobGender) {
        this.dobGender = dobGender;
    }

    public FamilyGivenName getFgn() {
        return fgn;
    }

    public void setFgn(FamilyGivenName fgn) {
        this.fgn = fgn;
    }
}

就像我之前说过的,这可以在一个更复杂的方式,但也会使用户更轻松地编写这么多类的编码。如果这不是您想要的路线,我可以删除这个路线并采取新的方法。如果你想让我延伸这个想法,我也可以这样做。

Like I said previously, This can be made in a much more complex way, but would also make coding of so many classes slightly easier for a user. If this is not the route you wanted to go, I can delete this one and take a new approach. If you want me to extend upon this idea, I can do that too.

我希望这会有所帮助。

编辑:

如果您只想调用Person并开始使用组件方法,这就是我所指的。

This is what I am referring to if you just want to call Person and start using the component methods.

    public class Person {

    DOBGender dobGender;
    FamilyGivenName fgn;

    public Person() {
        dobGender = new DOBGender();
        fgn = new FamilyGivenName();
    }

    public DOBGender getDobGender() {
        return dobGender;
    }


    public FamilyGivenName getFgn() {
        return fgn;
    }

}

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

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