Skip to content

Commit da4ce29

Browse files
committed
Inheritance lessons
1 parent 3f1c16c commit da4ce29

File tree

11 files changed

+324
-0
lines changed

11 files changed

+324
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
const Shape_1 = require("./Shape");
4+
const Circle_1 = require("./Circle");
5+
const Rectangle_1 = require("./Rectangle");
6+
let myShape = new Shape_1.Shape(10, 15);
7+
let myCircle = new Circle_1.Circle(5, 10, 20);
8+
let myRectangle = new Rectangle_1.Rectangle(0, 0, 3, 7);
9+
// declare an array of shapes ... initially empty
10+
let theShapes = [];
11+
// add the shapes to the array
12+
theShapes.push(myShape);
13+
theShapes.push(myCircle);
14+
theShapes.push(myRectangle);
15+
// theShapes.push("will this work?");
16+
// theShapes.push(1776) // won't work either because the array only accepts type of
17+
// the class Shape.
18+
for (let tempShape of theShapes) {
19+
console.log(tempShape.getInfo());
20+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { Shape } from './Shape';
2+
import { Circle } from './Circle';
3+
import { Rectangle } from './Rectangle';
4+
5+
let myShape = new Shape(10, 15);
6+
let myCircle = new Circle(5, 10, 20);
7+
let myRectangle = new Rectangle(0, 0, 3, 7);
8+
9+
// declare an array of shapes ... initially empty
10+
let theShapes: Shape[] = [];
11+
12+
// add the shapes to the array
13+
theShapes.push(myShape);
14+
theShapes.push(myCircle);
15+
theShapes.push(myRectangle);
16+
// theShapes.push("will this work?");
17+
// theShapes.push(1776) // won't work either because the array only accepts type of
18+
// the class Shape.
19+
20+
for (let tempShape of theShapes) {
21+
console.log(tempShape.getInfo());
22+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.Circle = void 0;
4+
const Shape_1 = require("./Shape");
5+
class Circle extends Shape_1.Shape {
6+
constructor(theX, theY, _radius) {
7+
super(theX, theY);
8+
this._radius = _radius;
9+
}
10+
get radius() {
11+
return this._radius;
12+
}
13+
set radius(value) {
14+
this._radius = value;
15+
}
16+
getInfo() {
17+
return super.getInfo() + `, radius=${this._radius}`;
18+
}
19+
}
20+
exports.Circle = Circle;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Shape } from './Shape';
2+
3+
export class Circle extends Shape {
4+
5+
constructor(theX: number, theY: number,
6+
private _radius: number) {
7+
8+
super(theX, theY);
9+
}
10+
11+
public get radius(): number {
12+
return this._radius;
13+
}
14+
15+
public set radius(value: number) {
16+
this._radius = value;
17+
}
18+
19+
getInfo(): string {
20+
return super.getInfo() + `, radius=${this._radius}`;
21+
}
22+
23+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
const Shape_1 = require("./Shape");
4+
const Circle_1 = require("./Circle");
5+
const Rectangle_1 = require("./Rectangle");
6+
let myShape = new Shape_1.Shape(10, 15);
7+
console.log(myShape.getInfo());
8+
let myCircle = new Circle_1.Circle(5, 10, 20);
9+
console.log(myCircle.getInfo());
10+
let myRectangle = new Rectangle_1.Rectangle(0, 0, 3, 7);
11+
console.log(myRectangle.getInfo());
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Shape } from './Shape';
2+
import { Circle } from './Circle';
3+
import { Rectangle } from './Rectangle';
4+
5+
let myShape = new Shape(10, 15);
6+
console.log(myShape.getInfo());
7+
8+
let myCircle = new Circle(5, 10, 20);
9+
console.log(myCircle.getInfo());
10+
11+
let myRectangle = new Rectangle(0, 0, 3, 7);
12+
console.log(myRectangle.getInfo());
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.Rectangle = void 0;
4+
const Shape_1 = require("./Shape");
5+
class Rectangle extends Shape_1.Shape {
6+
constructor(theX, theY, _width, _length) {
7+
super(theX, theY);
8+
this._width = _width;
9+
this._length = _length;
10+
}
11+
get width() {
12+
return this._width;
13+
}
14+
set width(value) {
15+
this._width = value;
16+
}
17+
get length() {
18+
return this._length;
19+
}
20+
set length(value) {
21+
this._length = value;
22+
}
23+
getInfo() {
24+
return super.getInfo() + `, width=${this._width}, length=${this._length}`;
25+
}
26+
}
27+
exports.Rectangle = Rectangle;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { Shape } from './Shape';
2+
3+
export class Rectangle extends Shape {
4+
5+
constructor(theX: number, theY: number,
6+
private _width: number, private _length: number) {
7+
8+
super(theX, theY);
9+
}
10+
11+
public get width(): number {
12+
return this._width;
13+
}
14+
15+
public set width(value: number) {
16+
this._width = value;
17+
}
18+
19+
public get length(): number {
20+
return this._length;
21+
}
22+
23+
public set length(value: number) {
24+
this._length = value;
25+
}
26+
27+
getInfo(): string {
28+
return super.getInfo() + `, width=${this._width}, length=${this._length}`;
29+
}
30+
31+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.Shape = void 0;
4+
class Shape {
5+
constructor(_x, _y) {
6+
this._x = _x;
7+
this._y = _y;
8+
}
9+
get x() {
10+
return this._x;
11+
}
12+
set x(value) {
13+
this._x = value;
14+
}
15+
get y() {
16+
return this._y;
17+
}
18+
set y(value) {
19+
this._y = value;
20+
}
21+
getInfo() {
22+
return `x=${this._x}, y=${this._y}`;
23+
}
24+
}
25+
exports.Shape = Shape;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
export class Shape {
2+
3+
constructor(private _x: number, private _y: number) {
4+
}
5+
6+
public get x(): number {
7+
return this._x;
8+
}
9+
10+
public set x(value: number) {
11+
this._x = value;
12+
}
13+
14+
public get y(): number {
15+
return this._y;
16+
}
17+
18+
public set y(value: number) {
19+
this._y = value;
20+
}
21+
22+
getInfo(): string {
23+
return `x=${this._x}, y=${this._y}`;
24+
}
25+
}

0 commit comments

Comments
 (0)