1

創建一個超級抽象類的具有不同數量的參數3個已經存在類似的子類我有3個班,我如何能在它們的構造

class ABCCache 
{ 
    private float paramA; 
    private float paramB; 

    public ABCCache(float paramA, float paramB) 
    { 
     this.paramA = paramA; 
     this.paramB = paramB; 
    } 

    @Override 
    public int hashCode() 
    { 
     final int prime = 31; 
     int result = 1; 
     result = prime * result + Float.floatToIntBits(paramA); 
     result = prime * result + Float.floatToIntBits(paramB); 
     return result; 
    } 

    @Override 
    public boolean equals(Object obj) 
    { 
     if (this == obj) 
      return true; 
     if (obj == null) 
      return false; 
     if (obj instanceof ABCCache) 
     { 
      ABCCache other = (ABCCache) obj; 
      return ((Float.floatToIntBits(paramA) == Float.floatToIntBits(other.paramA)) 
       && (Float.floatToIntBits(paramB) == Float.floatToIntBits(other.paramB))); 
     } 
     return false; 
    } 
} 

class DEFCache 
{ 
    private float paramD; 
    private float paramE; 
    private float paramF; 

    public DEFCache(float paramD, float paramE, float paramF) 
    { 
     this.paramD = paramD; 
     this.paramE = paramE; 
     this.paramF = paramF; 
    } 

    @Override 
    public int hashCode() 
    { 
     final int prime = 31; 
     int result = 1; 
     result = prime * result + Float.floatToIntBits(paramD); 
     result = prime * result + Float.floatToIntBits(paramE); 
     result = prime * result + Float.floatToIntBits(paramF); 
     return result; 
    } 

    @Override 
    public boolean equals(Object obj) 
    { 
     if (this == obj) 
      return true; 
     if (obj == null) 
      return false; 
     if (obj instanceof DEFCache) 
     { 
      DEFCache other = (DEFCache) obj; 
      return ((Float.floatToIntBits(paramD) == Float.floatToIntBits(other.paramD)) 
       && (Float.floatToIntBits(paramE) == Float.floatToIntBits(other.paramE)) 
       && (Float.floatToIntBits(paramF) == Float.floatToIntBits(other.paramF))); 
     } 
     return false; 
    } 
} 

class XYZCache 
{ 
    private float paramX; 

    public XYZCache(float paramX) 
    { 
     this.paramX = paramX; 
    } 

    @Override 
    public int hashCode() 
    { 
     final int prime = 31; 
     int result = 1; 
     result = prime * result + Float.floatToIntBits(paramX); 
     return result; 
    } 

    @Override 
    public boolean equals(Object obj) 
    { 
     if (this == obj) 
      return true; 
     if (obj == null) 
      return false; 
     if (obj instanceof XYZCache) 
     { 
      XYZCache other = (XYZCache) obj; 
      return (Float.floatToIntBits(paramX) == Float.floatToIntBits(other.paramX)); 
     } 
     return false; 
    } 
} 

上述所有類3存儲不同類型的緩存。

我有另一類PerformCalculation,如下所示:

public class PerformCalculation 
{ 
    public static void main(String[] args) 
    { 

    } 

    private float calculateABCValues(ABCCache abcCache) 
    { 
     //performOperation1 
     return // perform Operation 2; 
    } 

    private float calculateDEFValues(DEFCache defCache) 
    { 
     //performOperation1 
     return // perform Operation 2; 
    } 

    private float calculateXYZValues(XYZCache xyzCache) 
    { 
     //performOperation1 
     return // perform Operation 2; 
    } 
} 

有三種方法,其上的不同對象執行相同的操作。 對於刪除代碼重複,我想有一個方法,我可以通過任何三個對象。

所以我想創建一個父類具體類或它們的抽象類,我將作爲參數提供給單個「calculateValues(ParentCache緩存)」。

我不想創建一個空的父接口(標記接口)或AbstractClass,因爲它不會被推薦,只是爲了它而創建它們是不正確的。

如何創建這些子類的父項。

回答

2

您可以使用Java的運行時多態性作爲在下面的步驟解釋:

(1)定義緩存接口

public interface Cache { 
     T operation1(); 
     T operation2(); 
} 

(2)實現高速緩衝存儲器,實現緩存類接口

ABCCache implements Cache { 
    public T operation1() { 
    //code here 
    } 

    public T operation2() { 
     //code here 
    } 
} 

//Similarly implement other classes like DEFCache 

(3)定義的多態calculateValues(Cache cache)方法,採取任何緩存類型的對象

public class PerformCalculation { 

     public static void main(String[] args) 
     { 
      Cache cacheabc = new ABCCache(); 
      calculateValues(cacheabc);//calls ABCCache methods 

      Cache cachedef = new DEFCache(); 
      calculateValues(cachedef);//calls DEFCache methods 
     } 

     //input argument is Cache (interface) type, 
     //so takes any methods which implement Cache interface (like ABCCache, etc..) 
     private static float calculateValues(Cache cache) 
     { 
      cache.operation1(); 
      cache.operation2(); 
     } 
    } 

點,您需要在Java中學習(或OOP支持的語言)是爲了充分利用運行的編碼到接口多態性,我們可以通過在運行時傳遞不同的對象來實現動態行爲,如上所示。

換句話說,當你通過實現一個接口(稱爲編碼到接口),你會得到更多的靈活性,這樣你可以注入不同的對象(如你如何傳遞ABCCache對象等創建類..)在運行時的方法(它接受接口類型)。

您可以查看here瞭解相似的主題。

+0

感謝您的迴應Javaguy,但我知道運行時多態性,這裏的重點是沒有共同的操作,因爲我已經提到,有三個類,正如我在問題中提到的,沒有更多的代碼附在他們身上。 –

+0

如果沒有共同的行爲(操作),那麼根本就不需要將它們與Superclass/Subclass結合起來。 – developer

相關問題