2011-08-25 160 views
0

我有三種不同類型的參數:int,float和long。我想用一個對象來表示它們中的每一個。所以我有一個抽象類:泛型與子類

abstract public class AbstractProtocolParamObj<T extends Number> 
{ 
    public enum ProtocolParamConstraintTypeEnum 
    { 
     None, 
     OnOff, 
     Values, 
     ValueRange, 
     ValueRangeIncrement 
    } 

    protected String name; 
    protected ProtocolParamConstraintTypeEnum constraintValueType; 
    protected Vector<AbstractProtocolParamObj<T>> dependentParams; 
    protected Vector<AbstractProtocolParamObj<T>> constraintParams; 
    protected T value; 

    protected AbstractProtocolParamObj(String name, 
      ProtocolParamConstraintTypeEnum constraintValueType, 
      T value) 
    { 
     this.name = name; 
     this.constraintValueType = constraintValueType; 
     this.value = value; 
    } 

    public final String getName() 
    { 
     return name; 
    } 

    public final ProtocolParamConstraintTypeEnum getConstraintValueType() 
    { 
     return constraintValueType; 
    } 

    public void addDependentParam(AbstractProtocolParamObj<T> param) 
    { 
     if(dependentParams == null) 
     { 
      dependentParams = new Vector<AbstractProtocolParamObj<T>>(); 
     } 
     dependentParams.add(param); 
    } 

    public void addConstraintParam(AbstractProtocolParamObj<T> param) 
    { 
     if(constraintParams == null) 
     { 
      constraintParams = new Vector<AbstractProtocolParamObj<T>>(); 
     } 
     constraintParams.add(param); 
    } 

    public Vector<AbstractProtocolParamObj<T>> getDependentParams() 
    { 
     return dependentParams; 
    } 

    public Vector<AbstractProtocolParamObj<T>> getConstraintParams() 
    { 
     return constraintParams; 
    } 

    public T getValue() 
    { 
     return value; 
    } 

    public void setValue(T val) 
    { 
     value = val; 
    } 

    abstract public ReturnStatusEnum validate(T tempVal); 
} 

然後,我將有浮動參數的一類,一類爲int和一個長。就像這樣:

public class ProtocolFloatParamObj extends AbstractProtocolParamObj 
{ 
    private float[] constraintVals; 
    private float maxVal; 
    private float minVal; 
    private float increment; 

    public ProtocolFloatParamObj(String name, 
          float value, 
          float[] constraintVals, 
          ProtocolParamConstraintTypeEnum constraintType 
    ) 
    { 
     super(name, constraintType, value); 
     this.constraintVals = constraintVals; 
    } 

    public ProtocolFloatParamObj(String name, 
      float value, 
      float maxVal, 
      float minVal, 
      ProtocolParamConstraintTypeEnum constraintType 
    ) 
    { 
     super(name, constraintType, value); 
     this.maxVal = maxVal; 
     this.minVal = minVal; 
    } 

    public ProtocolFloatParamObj(String name, 
      float value, 
      float maxVal, 
      float minVal, 
      float increment, 
      ProtocolParamConstraintTypeEnum constraintType 
    ) 
    { 
     this(name, value, maxVal, minVal, constraintType); 
     this.increment = increment; 
    } 

    @Override 
    public ReturnStatusEnum validate(Number val) 
    { 
     ReturnStatusEnum status = ReturnStatusEnum.SUCCESS; 
     float tempVal = val.floatValue(); 

     switch(constraintValueType) 
     { 
      case None: 
      { 
       break; 
      } 
      case OnOff: 
      { 
       break; 
      } 
      case Values: 
      { 
            break; 
      } 
      case ValueRange: 
      { 
            break; 
      } 
      case ValueRangeIncrement: 
      { 
            break; 
      } 
     } 

     return status; 
    } 
} 

上面的代碼沒有編譯錯誤,但做了哪些抱怨泛型類型應該在子類在下面幾行進行參數設置警告:

public class ProtocolFloatParamObj extends AbstractProtocolParamObj 
super(name, constraintType, value); 

但如果我改變

public class ProtocolFloatParamObj extends AbstractProtocolParamObj 

public class ProtocolFloatParamObj <T extends Number> extends AbstractProtocolParamObj<T> 

並將構造函數value參數從float更改爲T。那麼沒有編譯錯誤和警告,一切看起來都很好

問題是因爲ProtocolFloatParamObj構造函數有一個T參數,它的用戶/調用者需要定義T,並且很容易得到警告或編譯錯誤。

例如,在另一個類,我嘗試創建一個向量包含其中的一些參數OBJ文件,但我不能消除警告或我無法添加到objs向量:

public Vector<AbstractProtocolParamObj<T>> getAxialParamObjs() 
    { 
     Vector<AbstractProtocolParamObj<T>> objs = new Vector<AbstractProtocolParamObj<T>>(); 
     ProtocolFloatParamObj<T> scanSpeed = new ProtocolFloatParamObj("scanSpeed", 
       new Float(Float.parseFloat(m_axialDefaultConfig.getProperty("scanSpeed"))), 
       new float[]{0.5f, 0.8f, 1.0f, 1.5f, 2.0f}, 
       ProtocolParamConstraintTypeEnum.Values 
       ); 

     objs.add(scanSpeed); 
     ...... 
     return objs; 
    } 

看來這不是一個好想法或我需要閱讀更多關於Java通用。

你有更好的主意,你有任何先進的Java通用教程鏈接?

+0

'ProtocolFloatParamObj'應該擴展'AbstractProtocolParamObj '。 – Marcelo

回答

1

ProtocolFloatParamObj <T extends Number> to only ProtocolFloatParamObj 

嘗試以下操作:

public class ProtocolFloatParamObj extends AbstractProtocolParamObj<Float> 

應該將Float指定爲超類的類型參數T.

+0

您的建議有效。謝謝。但如何消除以下代碼中的警告:Vector objs = new Vector ();我不能做以下事情,否則我不能添加任何ProtocolFloatParamObj到該向量中:Vector > objs = new Vector >(); – 5YrsLaterDBA

+0

你會得到什麼警告? –

0

變化

AbstractProtocolParamObj<T> to AbstractProtocolParamObj<Float> 

和改變,導致

public class ProtocolFloatParamObj extends AbstractProtocolParamObj<Float> 
0

爲什麼不這樣做是這樣的:

public class ProtocolFloatParamObj extends AbstractProtocolParamObj<Float>{ 
    .... 
}