javaclass

tests/DispatchTest.java

150:6c5a400aeffd
2005-01-23 Paul Boddie Fixed imports.
     1 public class DispatchTest {     2     public int a;     3     public float b;     4      5     public DispatchTest() {     6         this.a = 1;     7         this.b = 2;     8     }     9     10     public DispatchTest(int a) {    11         this.a = a;    12         this.b = 2;    13     }    14     15     public DispatchTest(float b) {    16         this.a = 1;    17         this.b = b;    18     }    19     20     public DispatchTest(int a, float b) {    21         this.a = a;    22         this.b = b;    23     }    24     25     public void set(int a) {    26         this.a = a;    27     }    28     29     public void set(float b) {    30         this.b = b;    31     }    32     33     // The ordering of the methods probably should not prevent the most specific    34     // method from being chosen, but this happens with a linear search of    35     // appropriate methods.    36     37     public int test(DispatchInterface obj) {    38         return obj.test();    39     }    40     41     public int test(DispatchClass1 obj) {    42         return obj.test() + 10;    43     }    44     45     public int testTest(DispatchInterface obj) {    46         return test(obj);    47     }    48     49     public static void main(String[] args) {    50         DispatchTest test = new DispatchTest();    51         DispatchClass1 dc1 = new DispatchClass1();    52         DispatchClass2 dc2 = new DispatchClass2();    53     54         if (test.a == 1 && test.b == 2) {    55             System.out.println("test.a, test.b correct: " + test.a + ", " + test.b);    56         } else {    57             System.err.println("test.a, test.b failed!");    58         }    59         test.set(5);    60         if (test.a == 5 && test.b == 2) {    61             System.out.println("test.a, test.b correct: " + test.a + ", " + test.b);    62         } else {    63             System.err.println("test.a, test.b failed!");    64         }    65         test.set(7.0f);    66         if (test.a == 5 && test.b == 7.0f) {    67             System.out.println("test.a, test.b correct: " + test.a + ", " + test.b);    68         } else {    69             System.err.println("test.a, test.b failed!");    70         }    71         if (test.test(dc1) == 11) {    72             System.out.println("test.test(dc1) correct: " + test.test(dc1));    73         } else {    74             System.err.println("test.test(dc1) failed!");    75         }    76         if (test.test(dc2) == 2) {    77             System.out.println("test.test(dc2) correct: " + test.test(dc2));    78         } else {    79             System.err.println("test.test(dc2) failed!");    80         }    81         // Yes, one might think this could be 11, but the parameter becomes    82         // "more vague" when passed to testTest.    83         if (test.testTest(dc1) == 1) {    84             System.out.println("test.testTest(dc1) correct: " + test.testTest(dc1));    85         } else {    86             System.err.println("test.testTest(dc1) failed!");    87         }    88         if (test.testTest(dc2) == 2) {    89             System.out.println("test.testTest(dc2) correct: " + test.testTest(dc2));    90         } else {    91             System.err.println("test.testTest(dc2) failed!");    92         }    93     }    94 }    95     96 interface DispatchInterface {    97     public int test();    98 }    99    100 class DispatchClass1 implements DispatchInterface {   101     public int test() {   102         return 1;   103     }   104 }   105    106 class DispatchClass2 implements DispatchInterface {   107     public int test() {   108         return 2;   109     }   110 }   111    112 // vim: tabstop=4 expandtab shiftwidth=4