OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2017, 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 library object_field_initializers_test; |
| 6 |
| 7 /// Simple program creating an object with instance field initializers in the |
| 8 /// declaration of the class. |
| 9 /// |
| 10 /// The static function `fieldInitializer` is used to ensure the fields are |
| 11 /// intialized in the correct order by tracking the order of side effects. |
| 12 void main() { |
| 13 var a1 = new A.withoutArguments(); |
| 14 print(a1.foo1); |
| 15 print(a1.foo2); |
| 16 print(a1.foo3); |
| 17 var a2 = new A('bar1', 'bar2'); |
| 18 print(a2.foo1); |
| 19 print(a2.foo2); |
| 20 print(a2.foo3); |
| 21 |
| 22 var b = new B(fieldInitializer(1, 'bar1'), fieldInitializer(2, 'bar2')); |
| 23 print(b.foo1); |
| 24 print(b.foo2); |
| 25 print(b.foo3); |
| 26 print(b.foo4); |
| 27 } |
| 28 |
| 29 class A { |
| 30 String foo1 = 'foo1'; |
| 31 String foo2; |
| 32 String foo3 = 'foo3'; |
| 33 |
| 34 A.withoutArguments(); |
| 35 A(String foo1, String foo2) { |
| 36 this.foo1 = foo1; |
| 37 this.foo2 = foo2; |
| 38 } |
| 39 } |
| 40 |
| 41 class B { |
| 42 String foo1 = fieldInitializer(3, 'foo1'); |
| 43 String foo2; |
| 44 String foo3 = fieldInitializer(4, 'foo3'); |
| 45 String foo4 = fieldInitializer(5, 'foo4'); |
| 46 |
| 47 B(String foo1, String foo2) { |
| 48 this.foo1 = foo1; |
| 49 this.foo2 = foo2; |
| 50 this.foo4 = fieldInitializer(6, 'bar4'); |
| 51 } |
| 52 } |
| 53 |
| 54 String fieldInitializer(int f, String s) { |
| 55 print('$s: $f'); |
| 56 return '$s: $f'; |
| 57 } |
OLD | NEW |