我有三种不同类型的参数: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通用教程链接?
'ProtocolFloatParamObj'应该扩展'AbstractProtocolParamObj'。 –
Marcelo