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