最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】VSCode中java代码警告:is a raw type References to generic type should be parameterized

Java crifan 4536浏览 0评论
折腾:
【未解决】VSCode中调试java报错:Build failed do you want to continue
期间,看到很多警告:
ContinuousAddressBuilder is a raw type. References to generic type ContinuousAddressBuilder<T> should be parameterized
点击看到代码是:
src/refer/java/iec_analysis/src/main/java/com/iec/assemble101/ContinuousAddressBuilder.java
    public ContinuousAddressBuilder builderCon(int con) {
        this.con = con;
        return this;
    }
is a raw type References to generic type should be parameterized
java – Raw type. References to generic types should be parameterized – Stack Overflow
去找到源码是:
src/refer/java/iec_analysis/src/main/java/com/iec/assemble101/ContinuousAddressBuilder.java
public class ContinuousAddressBuilder<T> extends VariableLengthPacket {
去搜此处项目中没有
extends ContinuousAddressBuilder
感觉此处不是很理解 语法和业务逻辑,不太会改代码
iterator – Java: Warning: References to generic type should be parameterized – Stack Overflow
再去搜:
extends VariableLengthPacket
只有此处2个类用到了:
public class ContinuousAddressBuilder<T> extends VariableLengthPacket {

没有其他extends VariableLengthPacket的类了。
所以,感觉只能改为:
// public ContinuousAddressBuilder builderCon(int con) {
public ContinuousAddressBuilder<VariableLengthPacket> builderCon(int con) {
但是却又报其他错了:
Type mismatch: cannot convert from ContinuousAddressBuilder<T> to ContinuousAddressBuilder<VariableLengthPacket>Java(16777235)
java – Why am I getting the warning :Class is a raw type. References to generic type Class<T> should be parameterized”? – Stack Overflow
说是可以关掉此警告:
@SuppressWarnings(“rawtypes”)
java – List is a raw type. References to generic type List<E> should be parameterized – Stack Overflow
参考:
“If there are multiple types of objects in your list than you can use a wildcard:
List<?> synchronizedpubliesdList = Collections.synchronizedList(publiesdList);”
试了试问号
public ContinuousAddressBuilder<?> builderCon(int con) {
语法上,倒是没有报错了:
不过貌似此处,不算是多个类型,而同一时刻只能有一个类型的
另外,去
Command+鼠标点击 T 看到定义:
-》可见:
当前类ContinuousAddressBuilder,之所以用了 <T>
是因为:内部有些变量或函数,支持多种变量类型:
/**
* 信息体元素。
* 包含多个元素,只接受int、float、double类型数据
*/
private ArrayList<T> informosomes;

public ContinuousAddressBuilder addInformosome(T informosome) {
  this.informosomes.add(informosome);
  return this;
}
-》所以此处类也是T
看到当前输入的变量是int,尝试改为:
public ContinuousAddressBuilder<int> builderCon(int con) {
结果报错:
去找找java中int的类型,或许是大写的Int? 或Integer ?
解决“List is a raw type. References to generic type List”提示的问题 – 大陶陶 – 博客园
    @SuppressWarnings("unchecked")
java – List is a raw type. References to generic type List<E> should be parameterized – Stack Overflow
List<String> publiesdList = new List<String>();
此处的String,就是我所说的类型
java – Eclipse warning – Class is a raw type. References to generic type Class<T> should be parameterized – Stack Overflow
Java int
Java 基本数据类型 | 菜鸟教程
java中short、int、long、float、double取值范围_qfikh的博客-CSDN博客
“int与Integer的基本使用对比
Integer是int的包装类;int是基本数据类型;
Integer变量必须实例化后才能使用;int变量不需要;
Integer实际是对象的引用,指向此new的Integer对象;int是直接存储数据值 ;
Integer的默认值是null;int的默认值是0。”
java int与integer的区别 – 发表是最好的记忆 – 博客园
此处java中int和Integer还是不一样的
无意间改为:
public ContinuousAddressBuilder<T> builderCon(int con) {
就没了警告了:
所以都去改为加上 <T>
【总结】
此处把代码:
public ContinuousAddressBuilder builderCon(int con) {
改为:
public ContinuousAddressBuilder<T> builderCon(int con) {
即可解决警告问题。

转载请注明:在路上 » 【已解决】VSCode中java代码警告:is a raw type References to generic type should be parameterized

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.177 seconds, using 22.10MB memory