Java Reference
In-Depth Information
return (value-32.0)*5.0/9.0;
}
};
TempConversion(String desc)
{
this.desc = desc;
}
private String desc;
@Override
public String toString()
{
return desc;
}
abstract double convert(double value);
public static void main(String[] args)
{
System.out.println(C2F+"
for
100.0
degrees
=
"+C2F.convert(100.0));
System.out.println(F2C+"
for
98.6
degrees
=
"+F2C.convert(98.6));
}
}
When you run this application, it generates the following output:
Celsius to Fahrenheit for 100.0 degrees = 212.0
Fahrenheit to Celsius for 98.6 degrees = 37.0
The Enum Class
The compiler regards
enum
as syntactic sugar. When it encounters an enum type de-
claration (
enum Coin {}
), it generates a class whose name (
Coin
) is specified by
thedeclaration,andwhichalsosubclassestheabstract
Enum
class(inthe
java.lang
package), the common base class of all Java-based enumeration types.
Ifyouexamine
Enum
'sJavadocumentation,youwilldiscoverthatitoverrides
Ob-
ject
's
clone()
,
equals()
,
finalize()
,
hashCode()
, and
toString()
methods: