我有 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)”的参数提供。
我不想创建一个空的父接口(interface)(标记接口(interface))或抽象类,因为不推荐这样做,并且仅仅为了它而创建它们是不正确的。
如何创建这些子类的父类。
最佳答案
您可以利用Java 的运行时多态性,如以下步骤所述:
(1)定义Cache接口(interface)
public interface Cache {
T operation1();
T operation2();
}
(2) 实现实现Cache接口(interface)的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 支持的语言)中,您需要学习的一点是对接口(interface)进行编码,以便利用运行时多态性,通过这种多态性,我们可以通过在运行时传递不同的对象来实现动态行为如上所示。
换句话说,当您通过实现接口(interface)来创建类(称为接口(interface)编码)时,您将获得更大的灵 active ,以便您可以注入(inject)不同的对象(比如如何在运行时将 ABCCache 对象传递给方法(接受接口(interface)类型)。
您可以查看here对于类似的主题。
关于java - 我如何创建一个由 3 个已经存在的类似子类组成的 super 抽象类,其构造函数中具有不同数量的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40677349/