繁体   English   中英

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

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

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

我想,如果我实现了一个列出了所有这些各种方法的接口,那么我只需调用/导入/引用该单个接口就可以使用所有方法? 但我被卡住了,因为这个解决方案似乎不起作用。

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

例如:有什么方法可以在同一个接口中实现 2 个不同的类,而每个类都没有抽象类? 好像该类是抽象的,那么我无法在下面的示例“应用程序”类中使用它的方法:

Common commonClass = new ABC_FamilyGivenName();

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

我有 2 个类,名为 ABC_FamilyGivenName 和 ABC_DOBGender。 我创建了一个接口 Common。

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

在当前的实现中,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() { }        
}

我可以避免上面的@Override 并只使用第一个示例中给出的没有这些的类吗?

Java中的面向对象编程需要“覆盖”所有方法,如果您正在实现一个方法,否则您可能会使用继承,因此并非所有方法都必须被覆盖。

在您的情况下,您可以将所有四个方法都放在父类 Base 中,然后继承它们。 那么不需要接口类或制作两个不同的接口。

要实现 Java 接口,您应该覆盖所有声明到接口中的抽象方法。 它是接口的基本概念。 这里interface Common所有四个方法都是抽象的,所以你应该覆盖它们。 否则,Java 编译器会抛出编译错误。 所以更好的方法可以将界面分成两部分。 实现接口的子类应该拥有接口的所有活动,这是接口的契约性质。 这是使用接口的主要目的。

如果你不想覆盖接口的所有方法,但你需要使用接口作为每个类的引用,那么你可以使用具体类而不是接口并将具体类继承到每个类

要实现以下代码更改,请确保您使用 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
    }

}

我可以避免上面的@Override 并只使用第一个示例中给出的没有这些的类吗?

不,在java中你必须实现接口的所有方法,除非它的抽象类

作为建议,您可以创建两个单独的界面,

有关更多详细信息,请参阅: 未实现接口的所有方法。 有可能吗?

您可以为称为 Adapter 类的其他类中的接口的所有方法提供空实现。 您可以在ABC_FamilyGivenName类和ABC_DOBGender类中扩展该适配器类。

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

 public void ABC_FamilyNames() {
 }

 public void ABC_Gender() {
 }

 public void ABC_BirthDay() {
 }
}

实施课程

        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