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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
|
//// [protectedMembers.ts]
// Class with protected members
class C1 {
protected x: number;
protected static sx: number;
protected f() {
return this.x;
}
protected static sf() {
return this.sx;
}
}
// Derived class accessing protected members
class C2 extends C1 {
protected f() {
return super.f() + this.x;
}
protected static sf() {
return super.sf() + this.sx;
}
}
// Derived class making protected members public
class C3 extends C2 {
x: number;
static sx: number;
f() {
return super.f();
}
static sf() {
return super.sf();
}
}
var c1: C1;
var c2: C2;
var c3: C3;
// All of these should be errors
c1.x;
c1.f();
C1.sx;
C1.sf();
// All of these should be errors
c2.x;
c2.f();
C2.sx;
C2.sf();
// All of these should be ok
c3.x;
c3.f();
C3.sx;
C3.sf();
class A {
protected x;
}
class B extends A {
y;
}
class C extends A {
z;
static foo(a: A, b: B, c: C, d: D, e: E) {
a.x = 1; // Error, access must be through C or type derived from C
b.x = 1; // Error, access must be through C or type derived from C
c.x = 1;
d.x = 1;
e.x = 1;
}
}
class D extends C {
d;
}
interface E extends C {
e;
}
class CC {
protected constructor() {
}
}
class A1 {
protected x;
}
class B1 {
x;
}
var a1: A1;
var b1: B1;
a1 = b1; // Error, B1 doesn't derive from A1
b1 = a1; // Error, x is protected in A1 but public in B1
class A2 {
protected x;
}
class B2 extends A2 {
x;
}
class A3 {
x;
}
// Error x is protected in B3 but public in A3
class B3 extends A3 {
protected x;
}
//// [protectedMembers.js]
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
// Class with protected members
var C1 = /** @class */ (function () {
function C1() {
}
C1.prototype.f = function () {
return this.x;
};
C1.sf = function () {
return this.sx;
};
return C1;
}());
// Derived class accessing protected members
var C2 = /** @class */ (function (_super) {
__extends(C2, _super);
function C2() {
return _super !== null && _super.apply(this, arguments) || this;
}
C2.prototype.f = function () {
return _super.prototype.f.call(this) + this.x;
};
C2.sf = function () {
return _super.sf.call(this) + this.sx;
};
return C2;
}(C1));
// Derived class making protected members public
var C3 = /** @class */ (function (_super) {
__extends(C3, _super);
function C3() {
return _super !== null && _super.apply(this, arguments) || this;
}
C3.prototype.f = function () {
return _super.prototype.f.call(this);
};
C3.sf = function () {
return _super.sf.call(this);
};
return C3;
}(C2));
var c1;
var c2;
var c3;
// All of these should be errors
c1.x;
c1.f();
C1.sx;
C1.sf();
// All of these should be errors
c2.x;
c2.f();
C2.sx;
C2.sf();
// All of these should be ok
c3.x;
c3.f();
C3.sx;
C3.sf();
var A = /** @class */ (function () {
function A() {
}
return A;
}());
var B = /** @class */ (function (_super) {
__extends(B, _super);
function B() {
return _super !== null && _super.apply(this, arguments) || this;
}
return B;
}(A));
var C = /** @class */ (function (_super) {
__extends(C, _super);
function C() {
return _super !== null && _super.apply(this, arguments) || this;
}
C.foo = function (a, b, c, d, e) {
a.x = 1; // Error, access must be through C or type derived from C
b.x = 1; // Error, access must be through C or type derived from C
c.x = 1;
d.x = 1;
e.x = 1;
};
return C;
}(A));
var D = /** @class */ (function (_super) {
__extends(D, _super);
function D() {
return _super !== null && _super.apply(this, arguments) || this;
}
return D;
}(C));
var CC = /** @class */ (function () {
function CC() {
}
return CC;
}());
var A1 = /** @class */ (function () {
function A1() {
}
return A1;
}());
var B1 = /** @class */ (function () {
function B1() {
}
return B1;
}());
var a1;
var b1;
a1 = b1; // Error, B1 doesn't derive from A1
b1 = a1; // Error, x is protected in A1 but public in B1
var A2 = /** @class */ (function () {
function A2() {
}
return A2;
}());
var B2 = /** @class */ (function (_super) {
__extends(B2, _super);
function B2() {
return _super !== null && _super.apply(this, arguments) || this;
}
return B2;
}(A2));
var A3 = /** @class */ (function () {
function A3() {
}
return A3;
}());
// Error x is protected in B3 but public in A3
var B3 = /** @class */ (function (_super) {
__extends(B3, _super);
function B3() {
return _super !== null && _super.apply(this, arguments) || this;
}
return B3;
}(A3));
|