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
        }
    }
4

4 回答 4

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是 type CoffeSize,其中有 method getLidCode()。它将按预期打印被覆盖方法的值。

于 2013-08-07T05:12:56.807 回答
1

我对enum's 不太熟悉,但我相信您的问题的答案是这样的:

有两种类型的 Lid,Lid A 和 B。由于 B 紧随咖啡标准尺寸的第三次声明 ( OVERWHELMING) 之后,每当getLidCode()调用 anCoffeeSize时,如果它匹配20,它将返回 Lid B。如果有任何其他手动设置的尺寸,例如 12 或 14,或BIGHUGE,它将返回A

所以基本上,盖子 B 意味着它是用于压倒性咖啡的标准盖子。如果顾客要 12 盎司、18 盎司或任何其他盎司的咖啡,或要 aBIGHUGE咖啡,他们会得到一个盖子 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"
于 2013-08-07T05:17:41.317 回答
1

方法getLidCode通常返回常量“A”。对于CoffeSize.BIGand CoffeSize.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() ;
    }
}

SUBTRACTION, DIVISION, 并且在调用POWER它们的方法时会抛出异常operate(由于某种原因,此时仅实现了可交换操作)。但是,BinaryOperation.ADDITION.operate(3.5,2.1)并且BinaryOperation.MULTIPLICATION.operate(4.5,2.0)将返回预期值(分别为 5.6 和 9.0)。这也回答了您关于使用的问题。是的,您的试探性示例是正确的。

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

于 2013-08-07T05:51:24.750 回答
0

Note that it is getOunce and not getOunces . The following is the correct usage .

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());

}

}

This would give the output as :

20

B

于 2013-08-07T05:20:26.487 回答