forked from AssemblyScript/assemblyscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-implements.ts
142 lines (124 loc) · 2.31 KB
/
class-implements.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
interface I {
foo(): i32;
}
interface J {
foo(): i32;
}
interface K extends J {
foo(): i32;
}
class A implements I {
foo(): i32 { return 1; }
}
var a = new A();
assert(a.foo() == 1);
class B {
}
class C extends B implements I {
foo(): i32 { return 2; }
}
var c = new C();
assert(c.foo() == 2);
class D implements I, J {
foo(): i32 { return 3; }
}
let d = new D();
assert(d.foo() == 3);
class E extends D implements I {
}
let e = new E();
assert(e.foo() == 3);
class F extends D implements I {
foo(): i32 { return 4; }
}
let f = new F();
assert(f.foo() == 4);
let g: D = new F();
assert(g.foo() == 4);
let h: I = new F();
assert(h.foo() == 4);
class G implements K {
foo(): i32 {
return 1;
}
}
assert((new G() as J).foo() == 1);
// can implement fields with fields or properties
interface I2 {
foo: i32;
}
class A2 implements I2 {
foo: i32 = 1; // implement
}
class A3 extends A2 {
foo: i32 = 5; // redeclare
}
class A4 extends A2 implements I2 {
foo: i32 = 7; // both
}
class B2 implements I2 {
// implement
private _foo: i32 = 3;
get foo(): i32 { return this._foo; }
set foo(foo: i32) { this._foo = foo; }
}
class B3 extends B2 {
// override
private _foo2: i32 = 9;
get foo(): i32 { return this._foo2; }
set foo(foo: i32) { this._foo2 = foo; }
}
class B4 extends B2 implements I2 {
// both
private _foo2: i32 = 11;
get foo(): i32 { return this._foo2; }
set foo(foo: i32) { this._foo2 = foo; }
}
{
let a2 = new A2();
assert(a2.foo == 1);
assert((<I2>a2).foo == 1);
(<I2>a2).foo = 2;
assert(a2.foo == 2);
assert((<I2>a2).foo == 2);
}
{
let a3 = new A3();
assert(a3.foo == 5);
assert((<I2>a3).foo == 5);
(<I2>a3).foo = 6;
assert(a3.foo == 6);
assert((<I2>a3).foo == 6);
}
{
let a4 = new A4();
assert(a4.foo == 7);
assert((<I2>a4).foo == 7);
(<I2>a4).foo = 8;
assert(a4.foo == 8);
assert((<I2>a4).foo == 8);
}
{
let b2 = new B2();
assert(b2.foo == 3);
assert((<I2>b2).foo == 3);
(<I2>b2).foo = 4;
assert(b2.foo == 4);
assert((<I2>b2).foo == 4);
}
{
let b3 = new B3();
assert(b3.foo == 9);
assert((<I2>b3).foo == 9);
(<I2>b3).foo = 10;
assert(b3.foo == 10);
assert((<I2>b3).foo == 10);
}
{
let b4 = new B4();
assert(b4.foo == 11);
assert((<I2>b4).foo == 11);
(<I2>b4).foo = 12;
assert(b4.foo == 12);
assert((<I2>b4).foo == 12);
}