折腾:
【未解决】用Java代码解析104协议收到的数据
期间,用VSCode调试java代码期间,看到代码有警告:
src/refer/java/iec_analysis/src/main/java/com/iec/analysis/common/TypeIdentifier.java
1 2 3 4 5 6 7 8 9 10 11 | public enum TypeIdentifier { private int code; private String hexStr; private String describe; TypeIdentifier( int code, String hexStr, String describe) { this.code = code; this.hexStr = hexStr; this.describe = describe; } |

对此问题,如果像之前一样简单处理,则就是:直接删除此行
但是问题在于:
此变量在enum初始化时还要用到的
1 2 3 4 5 | TypeIdentifier( int code, String hexStr, String describe) { this.code = code; this.hexStr = hexStr; this.describe = describe; } |
所以又不能删除
看到有 快速修复,点击试试

Remove xxx, keep assignments with side effects
试试效果:

1 2 3 4 5 6 7 8 | private int code; private String describe; TypeIdentifier( int code, String hexStr, String describe) { this.code = code; this.describe = describe; } |
很明显,此时:
enum初始化时,变量hexStr,没有用到。
不过,去研究了下代码,还是尽量用到此处的hexStr
期间:
【已解决】Java中实现字符串格式化和拼接
去把代码改为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | private int code; private String hexStr; private String describe; TypeIdentifier( int code, String hexStr, String describe) { this.code = code; this.hexStr = hexStr; this.describe = describe; } public static String getDescribe( int code) throws UnknownTypeIdentifierException { for (TypeIdentifier value : TypeIdentifier.values()) { if (value.code = = code) { / / String idDesc = value.hexStr + " " + value.describe; String idDesc = String. format ( "%s %s" , value.hexStr, value.describe); return idDesc; } } throw new UnknownTypeIdentifierException(); } |

即可。
这样,就能利用上hexStr了。
转载请注明:在路上 » 【已解决】VSCode中java代码警告:The value of the field is not used