Java Reference
In-Depth Information
TRY IT OUT: Embroidering an Enumeration
First, create a new directory, TryJackets , for the example and save the JacketSize.java file contain-
ing the definition of the enumeration from the previous section in it. Now create another file containing
the following definition:
public enum JacketColor { red, orange, yellow, blue, green }
Directory "TryJackets"
This should be in a file with the name JacketColor.java .
Now you can define a class that represents a jacket:
public class Jacket {
public Jacket(JacketSize size, JacketColor color) {
this.size = size;
this.color = color;
}
@Override
public String toString() {
StringBuffer str = new StringBuffer("Jacket ");
return str.append(size).append(" in ").append(color).toString();
}
private JacketSize size;
private JacketColor color;
}
Directory "TryJackets"
Finally, you need a file containing code to try out some jackets:
public class TryJackets {
public static void main(String[] args) {
// Define some jackets
Search WWH ::




Custom Search