提问人:Gianni Spear 提问时间:7/7/2023 最后编辑:Gianni Spear 更新时间:7/7/2023 访问量:352
Java 中参数化类的原始使用
Raw use of parameterized class in Java
问:
我有两个类在 Java 中使用泛型来执行度量单位之间的转换。在 BaseUnitConverter 类中,我有两个来自 intellij IDE 的警告,它们说:
Raw use of parameterized class 'InstantiatorUnitConverter'
Unchecked assignment: 'org.cnr.lambertools.utils.unitconverter.InstantiatorUnitConverter' to 'org.cnr.lambertools.utils.unitconverter.InstantiatorUnitConverter<T>'
Unchecked call to 'InstantiatorUnitConverter(Object, T, String)' as a member of raw type 'org.cnr.lambertools.utils.unitconverter.InstantiatorUnitConverter'
对于方法:
protected final <M,E> M storeValue(M t, double value, E tt){
me = new InstantiatorUnitConverter(value, tt, st(tt));
return t;
}
和
protected final <M, E> M storeValue(M t, Object value, E tt){
me = new InstantiatorUnitConverter(value, tt, st(tt));
return t;
}
此转换系统的基本类如下:
import java.io.Serializable;
/**
* Instantiated variables from new UnitConverter instance (Only used in class BaseUnitConverter).
*/
public final class InstantiatorUnitConverter<T> implements Serializable {
private final Object o; //Only used in DataType and NumericBase measurements (classes), user passed "from" value (Object). When this is the case, variable "v" is not used.
private final double v; //User passed "from" value (double). When this is the case, variable "o" is not used.
private final T t; //Enum constant value representing the "from" method of the measurement used.
private final String ts; //String value of enum constant representing the "from" method of the measurement used.
private final boolean d; //Used in class B.java "getValuePassed()". True if "from" value was a double, false for Object (Object only used in DataType and NumericBase measurements (classes)).
/**
* Empty constructor only called when instantiating from class B.java
*/
public InstantiatorUnitConverter(){
o = "";
v = -1;
t = null;
ts = "";
d = true;
}
/**
* Only used in class B.java when measurement "from" method only converts numbers.
* All measurements other than DataType and NumericBase use this constructor.
* @param value User passed "from" value (double).
* @param enumConstant Enum constant value representing the "from" method of the measurement used.
* @param enumTag String value of enum constant representing the "from" method of the measurement used.
*/
public InstantiatorUnitConverter(double value, T enumConstant, String enumTag){
o = ""; //This variable (Object value) will not be used when this constructor is called, default it to an empty string.
v = value;
t = enumConstant;
ts = enumTag;
d = true; //Variable d set to true as "from" value passed is a numeric type not an Object.
}
/**
* Only used in class B.java when measurement "from" method can convert more than numbers and an Object is passed as the value.
* Only measurements DataType and NumericBase use this constructor.
* @param value User passed "from" value (Object).
* @param enumConstant Enum constant value representing the "from" method of the measurement used.
* @param enumTag String value of enum constant representing the "from" method of the measurement used.
*/
public InstantiatorUnitConverter(Object value, T enumConstant, String enumTag){
o = value;
v = -1; //This variable (double value) will not be used when this constructor is called, default it to -1
t = enumConstant;
ts = enumTag;
d = false; //Variable d set to false as "from" value passed is an Object not double.
}
public Object getO() {
return o;
}
public double getV() {
return v;
}
public T getT() {
return t;
}
public String getTs() {
return ts;
}
public boolean isD() {
return d;
}
}
和
import java.io.Serializable;
/**
* Generic base class for most UnitConverter measurement classes.
*/
public class BaseUnitConverter<T> implements Serializable {
private InstantiatorUnitConverter<T> me; //Instantiate class A.java for variables needed to perform conversions.
public BaseUnitConverter() {
this.me = new InstantiatorUnitConverter<>();
}
public InstantiatorUnitConverter<T> getIstantiator(){
return this.me;
}
/**
* Returns value initially passed into the measurement's "from" method.
*/
public final Object getValuePassed(){
return (me.isD()) ? me.getV() : me.getO(); //true returns numeric value passed, false returns Object passed.
}
/**
* Returns the enum constant value representing the "from" method of the measurement used.
*/
public final String getTypeConstantPassed(){
return me.getTs();
}
/**
* Stores the needed values to do conversions of the measurement.
* This overload of the method is used in "from" methods in every measurement class other than Anything(), DataType(), and NumericBase().
*
* @param <M> Class context of measurement passed (usually "this" is passed from caller).
* @param <E> Enum constant value representing the "from" method of the measurement used.
* @param t Class context of measurement passed (usually "this" is passed from caller).
* @param value User passed "from" value (double).
* @param tt Enum constant value representing the "from" method of the measurement used.
* @return class context passed in so variable like "UnitOf.Length len" can be used as the variable type
*/
protected final <M,E> M storeValue(M t, double value, E tt){
me = new InstantiatorUnitConverter(value, tt, st(tt));
return t;
}
/**
* Stores the needed values to do conversions of the measurement.
* This overload of the method is used only in DataType and NumericBase as Objects can be passed as "from" values.
*
* @param <M> Class context of measurement passed (usually "this" is passed from caller).
* @param <E> Enum constant value representing the "from" method of the measurement used.
* @param t Class context of measurement passed (usually "this" is passed from caller).
* @param value User passed "from" value (Object).
* @param tt Enum constant value representing the "from" method of the measurement used.
* @return class context passed in so variable like "UnitOf.DataType dt" can be used as the variable type
*/
protected final <M, E> M storeValue(M t, Object value, E tt){
me = new InstantiatorUnitConverter(value, tt, st(tt));
return t;
}
/**
* Gets and returns the string value of constant representing the "from" method of the measurement used.
*/
private <E> String st(E tt){
String unitType = "";
try{
unitType = tt.toString();
} catch(Exception ignored){ }
return unitType;
}
/**
* Used by every measurement class that converts just numbers (Anything(), DataType(), NumericBase() do not apply here).
* Method performs the full conversion of taking the user defined "from" value and converting it into the user desired "to" value.
* @param a Enum constant value of "to" unit. Unit being converted into conversion constant value.
* @param b Enum constant value of "from" unit. Unit starting from conversion constant value.
* @return Finished conversion. "From" converted into "to" value.
*/
protected final double k(double a, double b){
return k(a,b,true);
}
/**
* Used by every measurement class that converts just numbers (Anything(), DataType(), NumericBase() do not apply here)
* Method performs the full conversion of taking the user defined "from" value and converting it into the user desired "to" value.
* @param a Enum constant value of "to" unit. Unit being converted into conversion constant value.
* @param b Enum constant value of "from" unit. Unit starting from conversion constant value.
* @param q Multiply then divide conversion algorithm, false will divide then multiply when converting "to"
* @return Finished conversion. "from" converted into "to" value.
*/
protected final double k(double a, double b, boolean q){
return UnitConverterUtils.i(UnitConverterUtils.i(me.getV(), a, q), b, !q);
}
}
我尝试了 Serg Vasylchak 提出的解决方案,但我收到一条错误消息(注意只是更改字母符号):
答:
0赞
Serg Vasylchak
7/7/2023
#1
protected final <M,E> M storeValue(M t, double value, E tt){
me = new InstantiatorUnitConverter<E>(value, tt, st(tt));
return t;
}
原始使用参数化类“AClass”意味着您有一个参数化定义,但引用它时没有“菱形”——class AClass<T>
AClass a = new AClass()
更多原始使用示例:
List list = new ArrayList<>(); // --> List<?> list
List<String> list = new ArrayList(); // --> new ArrayList<>();
Function function = o -> o.toString(); // --> Function<Object, String>
编辑
你有一个泛型类
class BaseUnitConverter<T> {
private InstantiatorUnitConverter<T> me;
protected final <M, E> M storeValue(M t, double value, E tt) {
this.me = new InstantiatorUnitConverter<E>(value, tt, st(tt));
return t;
}
}
问题在于将要分配给类型化的引用传递到类型化引用。E
T
这和你做的一样List<String> list = new ArrayList<Integer>()
;
我现在看到了 2 种修复方法,但这取决于您的具体情况。
- 使 storeValue 接受为参数(但您将失去类型的灵活性)
T
class BaseUnitConverter<T> {
private InstantiatorUnitConverter<T> me;
protected final <M> M storeValue(M t, double value, T tt) {
this.me = new InstantiatorUnitConverter<>(value, tt, st(tt));
return t;
}
}
- “De-generify”BaseUnitConverter 类(您必须在某个时候强制转换,就像它在 Java Collections 中的工作方式一样)
class BaseUnitConverter {
private InstantiatorUnitConverter<?> me;
protected final <M, E> M storeValue(M t, double value, E tt) {
this.me = new InstantiatorUnitConverter<>(value, tt, st(tt));
return t;
}
// You will lose the type here
public InstantiatorUnitConverter<?> getIstantiator(){
return this.me;
}
// Or generify the getter
// May throw java.lang.ClassCastException if referencing with the wrong type
// Java-collections-like style (e.g., see ArrayList)
@SuppressWarnings("unchecked")
public <T> InstantiatorUnitConverter<T> getIstantiator() {
return (InstantiatorUnitConverter<T>) this.me;
}
}
评论
0赞
Gianni Spear
7/7/2023
对不起,你的回答给我一个错误
0赞
Serg Vasylchak
7/7/2023
什么是“我”字段的类型?
0赞
Gianni Spear
7/7/2023
实例化器UnitConverter
0赞
Gianni Spear
7/7/2023
注意:使用答案中的错误更新问题
0赞
Gianni Spear
7/7/2023
private InstantiatorUnitConverter<T> me -> 创建一个 objct with public InstantiatorUnitConverter(){ o = “”;v = -1;t = 空;ts = “”;d = 真;}
评论