fork download
  1. import java.math.*;
  2.  
  3. class Ideone
  4. {
  5. public static void main (String[] args) {
  6. MM mm1 = new MM(BigDecimal.ONE);
  7. MM mm2 = new MM(BigDecimal.TEN);
  8. MM mm3 = mm1.add(mm2);
  9. System.out.println(mm3.getValue());
  10. }
  11.  
  12. static class Unit<T extends Unit<T>> {
  13. BigDecimal value;
  14.  
  15. public Unit(BigDecimal value) {
  16. this.value = value;
  17. }
  18.  
  19. public BigDecimal getValue() {
  20. return value;
  21. }
  22.  
  23. // First Arithmetic operation
  24. public <U extends Unit<U>> U add(U measurement) {
  25. value = value.add(measurement.getValue());
  26. return (U)this;
  27. }
  28. }
  29. static class MM extends Unit<MM> {
  30. public MM(BigDecimal value) {
  31. super(value);
  32. }
  33. }
  34. }
Success #stdin #stdout 0.06s 32828KB
stdin
Standard input is empty
stdout
11