2013-08-07 66 views
0

下面是一个来自SCJP 6示例的程序。在这里,我们用不同的咖啡大小创建一个enum,并声明一个名为ounces的私有变量来获得枚举的盎司值部分。枚举示例解释

我无法理解被覆盖的getLidCode方法的用法。如何访问getLidCode方法?

package com.chapter1.Declaration; 

enum CoffeSize { 
    BIG(8), HUGE(10), OVERWHELMING(20) { 
     public String getLidCode() { 
      return "B"; 
     } 
    }; 

    CoffeSize(int ounce) { 
     this.ounce = ounce; 
    } 

    private int ounce; 

    public int getOunce() { 
     return ounce; 
    } 

    public void setOunce(int ounce) { 
     this.ounce = ounce; 
    } 

    public String getLidCode() { 
     return "A"; 
    } 
} 

public class Prog7 { 

    CoffeeSize size; 

    public static void main(String[] args) { 

     Prog7 p = new Prog7(); 
      p.size = CoffeeSize.OVERWHELMING; 

      System.out.println(p.size.getOunces()); 

      //p.size.getLidCode(); ? is this correct way 
     } 
    } 
+0

简而言之,是的,这是正确的做法。 'p.size.getLidCode();'给你'B'。 – pad

回答

5

它更有意义,如果你的空间出来多一点:

enum CoffeSize { 
    BIG(8), 
    HUGE(10), 
    OVERWHELMING(20) { 
     public String getLidCode() { 
      return "B"; 
     } 
    }; 
    // rest of enum code here 
} 

只有OVERWHELMING是压倒getLidCode()

您将通过此方法(使用您的代码)访问:

System.out.println(p.size.getLidCode()); 

这样做的原因:p.size是类型CoffeSize,其中有方法getLidCode()。如预期的那样,它将打印重写的方法的值。

1

我不是太熟悉enum的,但我相信在回答你的问题是这样的:

有两种类型的盖子,盖子A和B的自B的第三个声明之后是标准尺寸的咖啡(OVERWHELMING),只要在CoffeeSize上调用getLidCode(),并且它匹配20,它将返回盖子B。如果有其他手动设置的尺寸,例如12或14,或者BIGHUGE,它将返回A

所以基本上,盖子B意味着它是压倒性咖啡的标准盖子。如果客户要求提供12盎司,或18盎司,或咖啡其他任何盎司,或为BIGHUGE咖啡,他们得到一个盖子A.

结论:

p.size = CoffeeSize.BIG; 
System.out.println(p.size.getOunces()); // Outputs "8" 
System.out.println(p.size.getLidType()); // Outputs "A" 

p.size = CoffeeSize.OVERWHELMING; 
System.out.println(p.size.getOunces()); // Outputs "20" 
System.out.println(p.size.getLidType()); // Outputs "B" 

p.size = CoffeeSize(12); 
System.out.println(p.size.getOunces()); // Outputs "12" 
System.out.println(p.size.getLidType()); // Outputs "A" 

p.setOunce(20); 
System.out.println(p.size.getOunces()); // Outputs "20" 
System.out.println(p.size.getLidType()); // Outputs "B" 
+0

你在正确的轨道上。唯一的问题是你不能以这种方式实例化一个枚举。 – Makoto

0

注意它是getOunce而不是getOunces。 以下是正确的用法。

public class Prog7 { 

CoffeSize size; 

public static void main(String[] args) { 

Prog7 p = new Prog7(); 
p.size = CoffeSize .OVERWHELMING; 

System.out.println(p.size.getOunce()); 

System.out.println(p.size.getLidCode()); 

}

}

这将使输出为:

1

方法getLidCode正常返回常数 “A”。对于CoffeSize.BIGCoffeSize.HUGE,它不会被覆盖,所以这是他们将返回的值。但是,对于CoffeSize.OVERWHELMING它被覆盖,它将返回“B”。

如果将枚举看作是类以及它们的枚举值作为该类的实例,则枚举允许在每个对象的基础上重写方法。这对于常规的类/对象是不可能的。

enum CoffeSize { 
    BIG(8,"A"), HUGE(10,"A"), OVERWHELMING(20,"B"); 

    CoffeSize(int ounce,String lidCode) { 
     this.ounce= ounce ; 
     this.lidCode= lidCode ; 
    } 

    private int ounce; 
    private String lidCode; 

    public int getOunce() { 
     return this.ounce ; 
    } 
    public void setOunce(int ounce) { 
     this.ounce= ounce ; 
    } 
    public String getLidCode() { 
     return this.lidCode ; 
    } 
} 

需要注意的是,使这个替代实现更相当于原来的一个,被定义没有setLidCode方法:

这可能是也已付诸实施。

这种机制的真实功率可以在下面的例子中更容易地理解的那样,虽然:

enum BinaryOperation { 
    ADDITION("+",1) { 
     public double operate(double a,double b) { 
      return a + b ; 
     } 
    }, 
    SUBTRACTION("-",1), 
    MULTIPLICATION("*",2) { 
     public double operate(double a,double b) { 
      return a * b ; 
     } 
    }, 
    DIVISION("/",2), 
    POWER("**",3); 

    BinaryOperation(String repr,int priority) { 
     this.repr= repr ; 
     this.priority= priority ; 
    } 

    private String repr; 
    private int priority; 

    public String toString() { 
     return this.repr ; 
    } 
    public int getPriority() { 
     return this.priority ; 
    } 
    public double operate(double a,double b) { 
     throw new UnsupportedOperationException() ; 
    } 
} 

SUBTRACTIONDIVISIONPOWER将抛出时被调用其operate方法异常(由于某种原因,此时只有交换操作已经实施)。但是,BinaryOperation.ADDITION.operate(3.5,2.1)BinaryOperation.MULTIPLICATION.operate(4.5,2.0)将返回预期值(分别为5.6和9.0)。这也回答你的使用问题。是的,你的尝试是正确的。

没有简单的OO方法来实现这个使用字段或其他机制。