github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/talks/2014/go4java/BadInheritance.java (about)

     1  import java.util.Collection;
     2  import java.util.ArrayList;
     3  
     4  class BadInheritance {
     5      // START_TASK OMIT
     6      class Task {
     7          private String message;
     8  
     9          public Task(String message) {
    10              this.message = message;
    11          }
    12  
    13          public void run() {
    14              System.out.println("running " + this.message);
    15          }
    16      }
    17      // END_TASK OMIT
    18  
    19      // START_RUNNER OMIT
    20      class Runner {
    21          private String name;
    22  
    23          public Runner(String name) {
    24              this.name = name;
    25          }
    26  
    27          public String getName() {
    28              return this.name;
    29          }
    30  
    31          public void run(Task task) { // HL
    32              task.run();
    33          }
    34  
    35          public void runAll(Task[] tasks) { // HL
    36              for (Task task : tasks) {
    37                  run(task);
    38              }
    39          }
    40      }
    41      // END_RUNNER OMIT
    42  
    43      // START_COUNTING OMIT
    44      class RunCounter extends Runner {
    45          private int count;
    46  
    47          public RunCounter(String message) {
    48              super(message);
    49              this.count = 0;
    50          }
    51  
    52          @Override public void run(Task task) {
    53              count++; // HL
    54              super.run(task);
    55          }
    56  
    57          @Override public void runAll(Task[] tasks) {
    58              count += tasks.length; // HL
    59              super.runAll(tasks);
    60          }
    61  
    62          public int getCount() {
    63              return count;
    64          }
    65      }
    66      // END_COUNTING OMIT
    67  
    68      public void test() {
    69          // START_MAIN OMIT
    70          RunCounter runner = new RunCounter("my runner");
    71  
    72          Task[] tasks = { new Task("one"), new Task("two"), new Task("three")};
    73          runner.runAll(tasks);
    74  
    75          System.out.printf("%s ran %d tasks\n", runner.getName(), runner.getCount());
    76          // END_MAIN OMIT
    77      }
    78  
    79      public static void main(String[] args) {
    80          new BadInheritance().test();
    81      }
    82  }