Java - Enum与Switch的比较 - 案例陈述

时间:2016-06-20 10:35:10

标签: java enums

我是Java编程语言的新手。所以我尝试了一个示例程序(即: - Enum Comparison with Switch-Case Statement)。以下是供您参考的计划

public class TestClass {
    public enum Company {
        Google(1),
        Microsoft(2),
        JPMorgan(3),
        WellsFargo(4);

        private int companyRatings;

        private Company(int companyValue) {
            this.companyRatings = companyValue;
        }
    }

    public static void enumComparison(Company type) {
        switch (type) {
            case Google:
                System.out.println("Company Name : " + type + " - Company Position : " + type.companyRatings);
            case Microsoft:
                System.out.println("Company Name : " + type + " - Company Position : " + type.companyRatings);
                break;
            case WellsFargo:
                System.out.println("Company Name : " + type + " - Company Position : " + type.companyRatings);
                break;
            default:
                System.out.println("Company Name : " + type + " - Company Position : " + type.companyRatings);
                break;
        }
    }

    public static void main(String[] args) {
        enumComparison(Company.Google);
    }
}

因此,如果您看到上述程序,您会注意到我在第一种情况下错过了break个关键字(i.e:- Case Google:)。在C#中,break关键字在switch语句中是必需的,如果错过关键字,则会导致编译时错误。但我在Java中注意到情况不是这样(No Compile time error for missing the break keyword)。所以在Main方法中我传递Company.Google作为参数。由于在第一种情况(Case Google:)中错过了break关键字,但它正在转移到第二种情况(Case Microsoft:)并打印该值,即使存在类型不匹配。这很奇怪,我不确定为什么会这样?所以它打印重复的值。我很困惑。

输出

Company Name : Google - Company Position : 1
Company Name : Google - Company Position : 1 

4 个答案:

答案 0 :(得分:1)

您可以将打印逻辑移动到toString()枚举的Company,并将您的switch语句修改为按类型分组公司。这显示了如何使用switch-case,而case每个break都有自己的public class TestClass { public enum Company { Google(1), Microsoft(2), JPMorgan(3), WellsFargo(4); private int companyRatings; private Company(int companyRatings) { this.companyRatings = companyRatings; } @Override public String toString() { return "Company Name : " + name() + " - Company Position : " + companyRatings; } } public static void enumComparison(Company type) { switch (type) { case Google: case Microsoft: System.out.println("[Technology Company]: " + type); break; case JPMorgan: case WellsFargo: System.out.println("[Investment Company]: " + type); break; default: System.out.println("[General... Company]: " + type); break; } } public static void main(String[] args) { enumComparison(Company.Google); } }

class Item(object):
    def __init__(self):
        self.c = 0
    def increase(self):
        S.increase(self)

class S(object):
    @staticmethod
    def increase(item):
        item.c += 1

答案 1 :(得分:0)

即使休息不是强制性的,但仍有许多情况下这样做是个好主意,例如在这里。

P.S。:您将companyRatings设置为private并稍后访问它,令我惊讶的是,这会编译。

答案 2 :(得分:0)

这是在java中定义switch语句的方式。代码从满足条件的case语句执行到break语句。

要理解为什么不需要break语句,请考虑以下示例:

  case Google:
  case Microsoft:
     System.out.println("Tech Company Name : " + type + " - Company Position : " + type.companyRatings);
  break;
  default:
   System.out.println("No Tech Company Name : " + type + " - Company Position : " + type.companyRatings);
                break;

如您所见,您可以为同一个块提供多个案例陈述。

答案 3 :(得分:0)

根本不奇怪。通过省略case语句中的break,您可以创建一些智能代码。这是一个小代码示例:

这是一个枚举

public enum Month{
    January(1),
    February(2),
    March(3),
    April(4),
    May(5),
    June(6),
    July(7),
    August(8),
    September(9),
    October(10),
    November(11),
    December(12);
}

这是我们通过当月的开关案例块

switch (currentMonth) {
        case January:
            System.out.println("Months to come " + Month.February);
        case February:
            System.out.println("Months to come " + Month.March);
        case March:
            System.out.println("Months to come " + Month.April);
        case April:
            System.out.println("Months to come " + Month.May);
        case May:
            System.out.println("Months to come " + Month.June);
        case June:
            System.out.println("Months to come " + Month.July);
        case July:
            System.out.println("Months to come " + Month.August);
        case August:
            System.out.println("Months to come " + Month.September);
        case September:
            System.out.println("Months to come " + Month.October);
        case October:
            System.out.println("Months to come " + Month.November);
        case November:
            System.out.println("Months to come " + Month.December);
        case December:
            System.out.println("This is the last month of the year");
            break;
        default:
            System.out.println("Invalid year");
            break;
    }

您可以通过查看输出来查看技巧。