OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 // Dart test for testing super operator calls | |
5 | |
6 | |
7 class A { | |
8 String val = ""; | |
9 List things; | |
10 | |
11 A() : things = ['D', 'a', 'r', 't', 42]; | |
12 | |
13 operator + (String s) { | |
14 val = "${val}${s}"; | |
15 return this; | |
16 } | |
17 | |
18 operator [] (i) { | |
19 return things[i]; | |
20 } | |
21 | |
22 operator []= (i, val) { | |
23 return things[i] = val; | |
24 } | |
25 } | |
26 | |
27 | |
28 class B extends A { | |
29 operator + (String s) { | |
30 super + ("${s}${s}"); // Call A.operator+(this, "${s}${s}"). | |
31 return this; | |
32 } | |
33 | |
34 operator [] (i) { | |
35 var temp = super[i]; | |
36 if (temp is String) { | |
37 return "$temp$temp"; | |
38 } | |
39 return temp + temp; | |
40 } | |
41 | |
42 operator []= (i, val) { | |
43 // Make sure the index expression is only evaluated | |
44 // once in the presence of a compound assignment. | |
45 return super[i++] += val; | |
46 } | |
47 | |
48 } | |
49 | |
50 main () { | |
51 var a = new A(); | |
52 a = a + "William"; // operator + of class A. | |
53 Expect.equals("William", a.val); | |
54 Expect.equals("r", a[2]); // operator [] of class A. | |
55 | |
56 a = new B(); | |
57 a += "Tell"; // operator + of class B. | |
58 Expect.equals("TellTell", a.val); | |
59 Expect.equals("rr", a[2]); // operator [] of class B. | |
60 | |
61 a[4] = 1; // operator []= of class B. | |
62 Expect.equals(43, a.things[4]); | |
63 Expect.equals(86, a[4]); | |
64 | |
65 } | |
OLD | NEW |