Effective Java™ (Jason Arnold's Library) by Joshua Bloch

Effective Java™ (Jason Arnold's Library) by Joshua Bloch

Author:Joshua Bloch
Language: eng
Format: epub
Publisher: Addison-Wesely
Published: 2008-02-15T00:00:00+00:00


public enum BasicOperation implements Operation {

PLUS("+") {

public double apply(double x, double y) { return x + y; }

},

MINUS("-") {

public double apply(double x, double y) { return x - y; }

},

TIMES("*") {

public double apply(double x, double y) { return x * y; }

},

DIVIDE("/") {

public double apply(double x, double y) { return x / y; }

};

private final String symbol;

BasicOperation(String symbol) {

this.symbol = symbol;

}

@Override public String toString() {

return symbol;

}

}

While the enum type (BasicOperation) is not extensible, the interface type (Operation) is, and it is the interface type that is used to represent operations in APIs. You can define another enum type that implements this interface and use instances of this new type in place of the base type. For example, suppose you want to define an extension to the operation type above, consisting of the exponentiation and remainder operations. All you have to do is write an enum type that implements the Operation interface:

// Emulated extension enum

public enum ExtendedOperation implements Operation {

EXP("^") {

public double apply(double x, double y) {

return Math.pow(x, y);

}

},

REMAINDER("%") {

public double apply(double x, double y) {

return x % y;

}

};



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.