简体   繁体   English

我如何创建3个超抽象类,它们已经在构造函数中提供了数量不同的相似子类

[英]How can i create a super abstract class of 3 already present similar subclasses having different number of parameters in their constructors

I have 3 classes, 我有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;
    }
}

All the 3 above classes stores cache of different types. 上面所有3个类都存储不同类型的缓存。

I have another class PerformCalculation as shown below: 我还有另一个类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;
    }
}

There are three methods which performs the same operations on different objects. 存在三种对不同对象执行相同操作的方法。 For removing code duplication, i want to have a single method where I can pass any of the three objects. 为了消除代码重复,我希望有一个方法可以传递三个对象中的任何一个。

So I thought of creating a parent concrete class or abstract class of them, which I will provide as an argument to the single "calculateValues(ParentCache cache)". 因此,我想到了创建它们的父具体类或抽象类的方法,我将作为单个“ calculateValues(ParentCache cache)”的参数来提供。

I dont to want to create an empty parent Interface(Marker Interface) or AbstractClass, as it wont be recommended and just creating them for the sake of it is not correct. 我不想创建一个空的父接口(标记接口)或AbstractClass,因为它不会被推荐,只是为了它们而创建它们是不正确的。

How can I create a parent of these Subclasses. 如何创建这些子类的父级。

You can make use of Java's runtime polymorphism as explained in the below steps: 您可以按照以下步骤中的说明使用Java的运行时多态性

(1) Define Cache interface (1)定义Cache接口

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

(2) Implement Cache classes which implement Cache interface (2)实现实现Cache接口的Cache类

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

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

//Similarly implement other classes like DEFCache

(3) Define polymorphic calculateValues(Cache cache) method which takes any Cache type objects (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();
       }
   }

One point you need to learn in Java (or OOP supported languages) is coding to interfaces in order to make use of runtime polymorphism by which we can achieve dynamic behavior by passing different objects at runtime as shown above. 您需要使用Java(或OOP支持的语言)学习的一点是对接口进行编码,以利用运行时多态性,通过这种方式,我们可以通过在运行时传递不同的对象来实现动态行为,如上所示。

In other words, when you create the classes by implementing an interface (called coding to interfaces), you will get more flexibility so that you can inject the different objects (like how you passed ABCCache object, etc..) at runtime to the method (which accepts the interface type). 换句话说,当您通过实现一个接口 (称为接口编码)来创建类时, 您将获得更大的灵活性,以便可以在运行时将不同的对象(如如何传递ABCCache对象等)注入该方法。 (接受接口类型)。

You can look at here for a similar subject matter. 您可以在这里查看类似的主题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM