简体   繁体   English

在 Java 中的同一个接口中实现多个类?

[英]Implement multiple classes in the same interface in Java?

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.现在的问题是,我希望在另一个类文件中使用所有这些方法(可能大约 200 个这样不同的类文件/方法),这些方法来自上述类文件的所有不同方法。

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.但我被卡住了,因为这个解决方案似乎不起作用。

The opposite of the above works (ie single class implements 2 interfaces: http://tutorials.jenkov.com/java/interfaces.html ).与上述工作相反(即单个类实现 2 个接口: 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 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?例如:有什么方法可以在同一个接口中实现 2 个不同的类,而每个类都没有抽象类? 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();

The above is not allowed, if the ABC_FamilyGivenName class is an abstract class.如果 ABC_FamilyGivenName 类是抽象类,则上述情况是不允许的。

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();
    }
}

I have 2 classes called ABC_FamilyGivenName & ABC_DOBGender.我有 2 个类,名为 ABC_FamilyGivenName 和 ABC_DOBGender。 I have created an interface Common.我创建了一个接口 Common。

I want to use the methods in both the above classes in another class called Application.我想在另一个名为 Application 的类中使用上述两个类中的方法。

With the current implementation, Java wants me to add an @Override to both the ABC_FamilyGivenName & ABC_DOBGender:在当前的实现中,Java 希望我向 ABC_FamilyGivenName 和 ABC_DOBGender 添加一个@Override:

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() { }        
}

Can I avoid the above @Override and just use the classes without these as given in the first example?我可以避免上面的@Override 并只使用第一个示例中给出的没有这些的类吗?

Object-oriented programming in Java requires to "override" all methods, if you are implementing a method, otherwise you may use inheritance, so not all methods must be overriden. Java中的面向对象编程需要“覆盖”所有方法,如果您正在实现一个方法,否则您可能会使用继承,因此并非所有方法都必须被覆盖。

In your case you may put all four methods to parent class Base and then inherit them.在您的情况下,您可以将所有四个方法都放在父类 Base 中,然后继承它们。 Then the interface class is not needed or make two different interfaces.那么不需要接口类或制作两个不同的接口。

To implement Java interface, You should override all the abstract methods are declared into the interface.要实现 Java 接口,您应该覆盖所有声明到接口中的抽象方法。 It is a basic concept of interface.它是接口的基本概念。 Here interface Common all four methods are abstract, So you should override them.这里interface Common所有四个方法都是抽象的,所以你应该覆盖它们。 Otherwise, Java compiler will throw a compilation error.否则,Java 编译器会抛出编译错误。 So better way can be splitting the interface into 2 parts.所以更好的方法可以将界面分成两部分。 It is a contractual nature of an interface the subclass who implement the interface should have all the activities of the interface.实现接口的子类应该拥有接口的所有活动,这是接口的契约性质。 It is the main purpose of using an interface.这是使用接口的主要目的。

If you don't wanna override all the method of interface but you need to use the interface as a reference of every class, then you can use a concrete class instead of interface and inherit the concrete class to every class如果你不想覆盖接口的所有方法,但你需要使用接口作为每个类的引用,那么你可以使用具体类而不是接口并将具体类继承到每个类

To implement the below code change please make sure you use java8要实现以下代码更改,请确保您使用 java8

public interface Common {

 default public void ABC_GivenNames() {
 }

 default public void ABC_FamilyNames() {
 }

 default public void ABC_Gender() {
 }

 default public 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
    }

}

Can I avoid the above @Override and just use the classes without these as given in the first example?我可以避免上面的@Override 并只使用第一个示例中给出的没有这些的类吗?

No, in java you have to implement all methods of interface unless its abstract class不,在java中你必须实现接口的所有方法,除非它的抽象类

as suggestion you can create two separate interfaces,作为建议,您可以创建两个单独的界面,

for more detail see : not implementing all of the methods of interface.有关更多详细信息,请参阅: 未实现接口的所有方法。 is it possible? 有可能吗?

You can provide an empty implementation for all the methods of an interface in other class called Adaptor class.您可以为称为 Adapter 类的其他类中的接口的所有方法提供空实现。 And you can extend that adaptor class in ABC_FamilyGivenName class and ABC_DOBGender class.您可以在ABC_FamilyGivenName类和ABC_DOBGender类中扩展该适配器类。

class Adaptor implements common
{
public void ABC_GivenNames() {
 }

 public void ABC_FamilyNames() {
 }

 public void ABC_Gender() {
 }

 public void ABC_BirthDay() {
 }
}

IMPLEMENTATION CLASSES :实施课程

        public class ABC_FamilyGivenName extends Adaptor{

            public void ABC_GivenNames(){
                // Implementation code
            }

            public void ABC_FamilyNames(){
                // Implementation code
            }

        }

        public class ABC_DOBGender extends Adaptor {

            public void ABC_Gender(){
                // Implementation code
            }

            public void ABC_BirthDay(){
                // Implementation code
            }

        }
interface Icalculate{                 //interface
calculate(operand1:number,operand2:number):number
}

class Add implements Icalculate{     //addition
calculate(operand1: number, operand2: number): number{
 return (operand1 + operand2);       
    }
}

 class Sub implements Icalculate{       //subtraction
 calculate(operand1: number, operand2: number): number{
    return (operand1 - operand2);
  }
 }

class Mul implements Icalculate{           //multiplicationn
calculate(operand1: number, operand2: number): number{
    return(operand1*operand2);
 }
}

 class Div implements Icalculate{         //Division
  calculate(operand1: number, operand2: number): number{
    return(operand1/operand2);
   }
}  
  let a = new Add;
  let b = new Sub;
  let c = new Mul;
  let d = new Div;

  class Calculator {                   //main class
    operator: Icalculate;
    operand1: number;
    operand2: number;
    constructor(a: number, b: number, operator: Icalculate) {
    this.operand1 = a;
    this.operand2 = b;
    this.operator = operator;
    let op = this.operator;
    console.log(op.calculate(this.operand1, this.operand2));
    }
}   
const cal=new Calculator(1,1,a);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM