java 引用一个类,实现一个引用多个类的Java类/接口

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.

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.

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?

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

Creating a Class which has all the other class declared in it

Creating an interface, which has implementations across the multiple classes (the ~200+ mentioned above)

Implemented a class and an interface

Nope of the ways seem to work.

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

Example (this is not the right code or implementation, just wanted to provide an example of what we want to do in code):

I have 2 classes called "ABC_FamilyGivenName" & "ABC_DOBGender".

I have created an interface "Common".

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

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.

I hope this helps.

EDIT:

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;

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值