[英]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.