Esempio n. 1
0
 public static void main(String[] args) {
   Set<Watercolors> set1 = EnumSet.range(BRILLIANT_RED, VIRIDIAN_HUE);
   Set<Watercolors> set2 = EnumSet.range(CERULEAN_BLUE_HUE, BURNT_UMBER);
   print("set1: " + set1);
   print("set2: " + set2);
   print("union(set1, set2): " + union(set1, set2));
   Set<Watercolors> subset = intersection(set1, set2);
   print("intersection(set1, set2): " + subset);
   print("difference(set1, subset): " + difference(set1, subset));
   print("difference(set2, subset): " + difference(set2, subset));
   print("complement(set1, set2): " + complement(set1, set2));
 }
Esempio n. 2
0
 public String toString() {
   return cycles.toString();
 }
Esempio n. 3
0
 private void add(Cycle cycle) {
   cycles.add(cycle);
 }
Esempio n. 4
0
public class CarWash {
  /** 常量相关方法,比匿名内部类更高效 */
  public void wash() {
    CarWash wash = new CarWash();
    print(wash);
    wash.washCar();
    // Order of addition is unimportant:
    wash.add(Cycle.BLOWDRY);
    wash.add(Cycle.BLOWDRY); // Duplicates ignored
    wash.add(Cycle.RINSE);
    wash.add(Cycle.HOTWAX);
    print(wash);
    wash.washCar();
  }

  public enum Cycle {
    UNDERBODY {
      void action() {
        print("Spraying the underbody");
      }
    },
    WHEELWASH {
      void action() {
        print("Washing the wheels");
      }
    },
    PREWASH {
      void action() {
        print("Loosening the dirt");
      }
    },
    BASIC {
      void action() {
        print("The basic wash");
      }
    },
    HOTWAX {
      void action() {
        print("Applying hot wax");
      }
    },
    RINSE {
      void action() {
        print("Rinsing");
      }
    },
    BLOWDRY {
      void action() {
        print("Blowing dry");
      }
    };

    abstract void action();
  }

  EnumSet<Cycle> cycles = EnumSet.of(Cycle.BASIC, Cycle.RINSE);

  private void add(Cycle cycle) {
    cycles.add(cycle);
  }

  private void washCar() {
    for (Cycle c : cycles) c.action();
  }

  public String toString() {
    return cycles.toString();
  }
} /*