这个例子体现了aop但是怎么体会不到指点下?

java 代码
  1. //: generics/Mixins.java
  2. import java.util.*;
  3. interface TimeStamped { long getStamp(); }

  4. class TimeStampedImp implements TimeStamped {
  5. private final long timeStamp;
  6. public TimeStampedImp() {
  7. timeStamp = new Date().getTime();
  8. }
  9. public long getStamp() { return timeStamp; }
  10. }

  11. interface SerialNumbered { long getSerialNumber(); }

  12. class SerialNumberedImp implements SerialNumbered {
  13. private static long counter = 1;
  14. private final long serialNumber = counter++;
  15. public long getSerialNumber() { return serialNumber; }
  16. }

  17. interface Basic {
  18. public void set(String val);
  19. public String get();
  20. }

  21. class BasicImp implements Basic {
  22. private String value;
  23. public void set(String val) { value = val; }
  24. public String get() { return value; }
  25. }

  26. class Mixin extends BasicImp
  27. implements TimeStamped, SerialNumbered {
  28. private TimeStamped timeStamp = new TimeStampedImp();
  29. private SerialNumbered serialNumber =
  30. new SerialNumberedImp();
  31. public long getStamp() { return timeStamp.getStamp(); }
  32. public long getSerialNumber() {
  33. return serialNumber.getSerialNumber();
  34. }
  35. }

  36. public class Mixins {
  37. public static void main(String[] args) {
  38. Mixin mixin1 = new Mixin(), mixin2 = new Mixin();
  39. mixin1.set("test string 1");
  40. mixin2.set("test string 2");
  41. System.out.println(mixin1.get() + " " +
  42. mixin1.getStamp() + " " + mixin1.getSerialNumber());
  43. System.out.println(mixin2.get() + " " +
  44. mixin2.getStamp() + " " + mixin2.getSerialNumber());
  45. }
  46. } /* Output: (Sample)
  47. test string 1 1132437151359 1
  48. test string 2 1132437151359 2
  49. *///:~

你可能感兴趣的:(AOP)