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

相关问题