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 | |
5 class GettersSettersTest { | |
6 | |
7 static int foo; | |
8 | |
9 static get bar() { | |
10 return foo; | |
11 } | |
12 | |
13 static set bar(newValue) { | |
14 foo = newValue; | |
15 } | |
16 | |
17 static testMain() { | |
18 A a = new A(); | |
19 a.x = 2; | |
20 Expect.equals(2, a.x); | |
21 Expect.equals(2, a.x_); | |
22 | |
23 // Test inheritance. | |
24 a = new B(); | |
25 a.x = 4; | |
26 Expect.equals(4, a.x); | |
27 Expect.equals(4, a.x_); | |
28 | |
29 // Test overriding. | |
30 C c = new C(); | |
31 c.x = 8; | |
32 Expect.equals(8, c.x); | |
33 Expect.equals(0, c.x_); | |
34 Expect.equals(8, c.y_); | |
35 | |
36 // Test keyed getters and setters. | |
37 a.x_ = 0; | |
38 Expect.equals(2, a[2]); | |
39 a[2] = 4; | |
40 Expect.equals(6, a[0]); | |
41 | |
42 // Test assignment operators. | |
43 a.x_ = 0; | |
44 a[2] += 8; | |
45 Expect.equals(12, a[0]); | |
46 | |
47 // Test calling a function that internally uses getters. | |
48 Expect.equals(true, a.isXPositive()); | |
49 | |
50 // Test static fields. | |
51 foo = 42; | |
52 Expect.equals(42, foo); | |
53 Expect.equals(42, bar); | |
54 A.foo = 43; | |
55 Expect.equals(43, A.foo); | |
56 Expect.equals(43, A.bar); | |
57 | |
58 bar = 42; | |
59 Expect.equals(42, foo); | |
60 Expect.equals(42, bar); | |
61 A.bar = 43; | |
62 Expect.equals(43, A.foo); | |
63 Expect.equals(43, A.bar); | |
64 } | |
65 } | |
66 | |
67 class A { | |
68 A(); | |
69 int x_; | |
70 static int foo; | |
71 | |
72 static get bar() { | |
73 return foo; | |
74 } | |
75 | |
76 static set bar(newValue) { | |
77 foo = newValue; | |
78 } | |
79 | |
80 int get x() { | |
81 return x_; | |
82 } | |
83 | |
84 void set x(int value) { | |
85 x_ = value; | |
86 } | |
87 | |
88 bool isXPositive() { | |
89 return x > 0; | |
90 } | |
91 | |
92 int operator [](int index) { | |
93 return x_ + index; | |
94 } | |
95 | |
96 void operator []=(int index, int value) { | |
97 x_ = index + value; | |
98 } | |
99 | |
100 int getX_() { | |
101 return x_; | |
102 } | |
103 } | |
104 | |
105 class B extends A { | |
106 B(); | |
107 } | |
108 | |
109 class C extends A { | |
110 int y_; | |
111 | |
112 C() { | |
113 this.x_ = 0; | |
114 } | |
115 | |
116 int get x() { | |
117 return y_; | |
118 } | |
119 | |
120 void set x(int value) { | |
121 y_ = value; | |
122 } | |
123 } | |
124 | |
125 main() { | |
126 GettersSettersTest.testMain(); | |
127 } | |
OLD | NEW |