java 不识别enum,我的方法无法识别Java枚举类型

Currently developing a code smell detector for a uni assignment. I have made an abstract CodeSmell class, with two concrete subclasses - ClassLevelSmell and MethodLevelSmell. The CodeSmell class has a protected field of type SmellType. A sample constructor is as follows:

public ClassLevelSmell(ClassDefinition classDef, SmellType type){

smellyClass = classDef;

this.smell = type;

}

SmellType is an enum I defined, which looks like this:

public enum SmellType {

LONG_CLASS, LONG_METHOD, PRIMITIVE_OBSESSION, LONG_PARAM_LIST}

I then have a SmellDetector object, with numerous methods that compare the statistics of analyzed classes and methods (such as their number of lines, number of primitive declarations etc) and creates a new CodeSmell object if a smell is found. So my code for this looks like this:

private void detectLongClass(ClassDefinition classDef) {

if(classDef.getNumLines() > 250){

smells.add(new ClassLevelSmell(classDef, LONG_CLASS));

}

}

Each SmellDetector object has a field smells, an ArrayList of CodeSmells. However, I'm getting a compiler warning in eclipse when I try to pass the SmellType LONG_CLASS into the constructor for the ClassLevelMethod, telling me "LONG_CLASS cannot be resolved to a variable". Am I making some mistake with the use of Enumerated types? What do?

解决方案

To reference enum values you either need to use the qualified form with the class name or use static imports. So either make it:

smells.add(new ClassLevelSmell(classDef, SmellType.LONG_CLASS));

or do this:

// at the top of your file:

import static packagename.SmellType.*;

smells.add(new ClassLevelSmell(classDef, LONG_CLASS));

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值