2016-08-24 134 views
-2

以下代码在编译和运行时没有问题。但我的问题是什么是公共int getValue(int bid) [NOT getValues(int bid)?我将如何调用该方法?Java枚举方法

package com.main; 
public class Bridge { 

    public enum Suites{ 
     CLUB(20), DIMOND(20), HEARTS(30), SPADES(40){ 
      public int getValue(int bid){ 
       return ((bid-1)*30)+40; 
      } 
     }; 
     private int points; 
     Suites(int points){ 
      this.points = points; 

     } 

     public int getValues(int bid){ 
      return points*bid; 
     } 

    } 

    public static void main(String[] args){ 
     System.out.println(Suites.CLUB.getValues(3)); 
     System.out.println(Suites.HEARTS.points); 
     System.out.println(Suites.values()); 
    } 

} 
 
The out put is : 
60 
30 
[Lcom.main.Bridge$Suites;@2a139a55 
+3

它没有任何意义,不能叫,除了通过反射。这可能是一个错字,我正在投票结束。 –

+0

英文中,该类别被称为“适合”,其值为“黑桃”,“俱乐部”,“钻石”和“心脏”。你在那里有一些错别字。 – Andreas

回答

0

唯一用例用于指定SPADES getValue方法是如果用于套房枚举,例如被指定具有相同的和和签名的方法像这样:

public class Test { 

    public enum Suites { 
     CLUB(20), 
     DIMOND(20), 
     HEARTS(30), 
     SPADES(40) { 
      @Override 
      public int getValue(int bid) { 
       return ((bid - 1) * 30) + 40; 
      } 
     }; 
     private int points; 

     Suites(int points) { 
      this.points = points; 

     } 

     public int getValue(int bid) { 
      return bid; 
     } 

     public int getValues(int bid) { 
      return points * bid; 
     } 

    } 

    public static void main(String[] args) { 
     for (Suites suite : Suites.values()) { 
      System.out.println(suite.name() + ": getValue: " + suite.getValue(1)); 
     } 
    } 
} 

这将输出

CLUB: getValue: 1 
DIMOND: getValue: 1 
HEARTS: getValue: 1 
SPADES: getValue: 40