| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 // Test that static members cannot be overridden (static type warning only). | 5 // Test the semantics of static members mixed with instance members. |
| 6 | 6 |
| 7 m() {} | 7 // Following are relevant quotes from Dart Programming Language |
| 8 // Specification, Draft Version 0.10, June 7, 2012. |
| 9 |
| 10 // 7 Classes: |
| 11 |
| 12 // "It is a compile-time error if a class has an instance method and a |
| 13 // static member method with the same name." |
| 14 |
| 15 // 7.1 Instance Methods: |
| 16 |
| 17 // "Instance methods are functions (6) whose declarations are |
| 18 // immediately contained within a class declaration and that are not |
| 19 // declared static. The instance methods of a class C are those |
| 20 // instance methods declared by C and the instance methods inherited |
| 21 // by C from its superclass." |
| 22 |
| 23 |
| 24 // 7.6 Static Methods |
| 25 |
| 26 // "Static methods are functions whose declarations are immediately |
| 27 // contained within a class declaration and that are declared |
| 28 // static. The static methods of a class C are those static methods |
| 29 // declared by C." |
| 30 |
| 31 // 7.7 Static Variables |
| 32 |
| 33 // "Static variables are variables whose declarations are immediately |
| 34 // contained within a class declaration and that are declared |
| 35 // static. The static variables of a class C are those static |
| 36 // variables declared by C." |
| 37 |
| 38 // "A static variable declaration of one of the forms static T v;, |
| 39 // static T v = e; , static const T v = e; or static final T v = e; |
| 40 // always induces an implicit static getter function (7.2) with |
| 41 // signature static T get v() whose invocation evaluates as described |
| 42 // below (7.7.1)." |
| 43 |
| 44 m() => 'top level'; |
| 8 | 45 |
| 9 class Super { | 46 class Super { |
| 10 Super() {} | |
| 11 // No error from hiding. | 47 // No error from hiding. |
| 12 static m() {} | 48 static m() => 'super'; |
| 13 | 49 |
| 14 static var i; | 50 static var i = 'super'; |
| 15 | 51 |
| 16 instanceMethod() {} | 52 static var i2 = 'super'; |
| 53 |
| 54 instanceMethod() => m(); |
| 55 |
| 56 instanceMethod2() => m(); |
| 17 } | 57 } |
| 18 | 58 |
| 19 class Sub extends Super { | 59 class Sub extends Super { |
| 20 Sub() : super(); | 60 // According to 7.6, static methods are not inherited. |
| 21 static m() {} /// 01: static type warning | 61 static m() => 'sub'; |
| 22 | 62 |
| 23 static var i; /// 02: static type warning | 63 // According to 7.7, static variables are not inherited. |
| 64 static var i = 'sub'; |
| 24 | 65 |
| 25 static instanceMethod() {} /// 03: compile-time error | 66 // According to 7.1, instance methods include those of the |
| 67 // superclass, and according to 7, it is a compile-time to have an |
| 68 // instance method and static method with the same name. |
| 69 static /// 03: compile-time error |
| 70 instanceMethod() => m(); |
| 26 | 71 |
| 27 static i() {} /// 04: static type warning | 72 // According to 7.7, static variables are not inherited. |
| 73 static i2() => m(); |
| 28 | 74 |
| 29 static var instanceMethod; /// 05: compile-time error | 75 // According to 7.1, instance methods include those of the |
| 76 // superclass, and according to 7, it is a compile-time to have an |
| 77 // instance method and static method with the same |
| 78 // name. Furthermore, according to 7.7 a static variable induces an |
| 79 // implicit getter function (a static method). |
| 80 static var instanceMethod2; /// 05: compile-time error |
| 30 | 81 |
| 31 foo() {} | 82 foo() => 'foo'; |
| 32 } | 83 } |
| 33 | 84 |
| 34 main() { | 85 main() { |
| 35 new Sub().foo(); | 86 Expect.equals('foo', new Sub().foo()); |
| 87 Expect.equals('top level', m()); |
| 88 Expect.equals('super', Super.m()); |
| 89 Expect.equals('sub', Sub.m()); |
| 90 Expect.equals('super', Super.i); |
| 91 Expect.equals('sub', Sub.i); |
| 92 Expect.equals('super', Super.i2); |
| 93 Expect.equals('sub', Sub.i2()); |
| 94 Expect.equals('super', new Super().instanceMethod()); |
| 95 Expect.equals('sub', new Sub().instanceMethod()); |
| 96 Expect.equals('super', new Super().instanceMethod2()); |
| 97 Expect.equals('super', new Sub().instanceMethod2()); |
| 36 } | 98 } |
| OLD | NEW |