[英]How can i create a super abstract class of 3 already present similar subclasses having different number of parameters in their constructors
我有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 cache)”的參數來提供。
我不想創建一個空的父接口(標記接口)或AbstractClass,因為它不會被推薦,只是為了它們而創建它們是不正確的。
如何創建這些子類的父級。
您可以按照以下步驟中的說明使用Java的運行時多態性 :
(1)定義Cache接口
public interface Cache {
T operation1();
T operation2();
}
(2)實現實現Cache接口的Cache類
ABCCache implements Cache {
public T operation1() {
//code here
}
public T operation2() {
//code here
}
}
//Similarly implement other classes like DEFCache
(3)定義多態的calculateValues(Cache 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對象等)注入該方法。 (接受接口類型)。
您可以在這里查看類似的主題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.