Skip to content

Instantly share code, notes, and snippets.

@codeBelt codeBelt/Animal.ts
Created May 31, 2014

Embed
What would you like to do?
TypeScript Classes and OOP examples (tsc TestApp.ts -out app.js)
module MyNamespace {
export class Animal {
public furry:boolean;
public domestic:boolean;
constructor() {
console.log("new animal created");
}
}
}
module MyNamespace {
export class Brick {
public color:string = 'red';
constructor() {
console.log('new '+ this.color +' brick created');
}
}
}
///<reference path='Animal.ts'/>
module MyNamespace {
export class Cat extends Animal {
public family:string;
constructor() {
super();
this.furry = true;
this.domestic = true;
this.family = "feline";
}
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<title>TypeScript is Awesome</title>
</head>
<body>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="assets/scripts/app.js"></script>
<script>
$(document).ready(function(event) {
// Get class from namespace.
var TestApp = AnotherNamespace.TestApp;
var app = new TestApp();
});
</script>
</body>
</html>
module MyNamespace {
export interface IWork {
work():void;
}
}
module MyNamespace {
export class Person {
private _age:number = 1;
constructor() {
}
public get age():number {
return this._age;
}
public set age(val:number) {
if(this._age > 0) {
this._age = val;
}
}
}
}
module MyNamespace {
export class Student implements IWork {
public averageGrade:number;
constructor() {
this.averageGrade = 4.07;
}
public work():void {
console.log("I am studying");
}
}
}
///<reference path='Teacher.ts'/>
///<reference path='Student.ts'/>
///<reference path='IWork.ts'/>
module MyNamespace {
export class Supervisor {
constructor(inst:IWork) {
inst.work();
if(inst instanceof Teacher) {
var teacher:Teacher = <Teacher>inst;
console.log('Has tenure:', teacher.tenure);
}
if(inst instanceof Student) {
var student:Student = <Student>inst;
console.log('Average grade is:', student.averageGrade);
}
}
}
}
module MyNamespace {
export class Teacher implements IWork {
public tenure:boolean;
constructor() {
this.tenure = true;
}
public work():void {
console.log("I am teaching");
}
}
}
///<reference path='Brick.ts'/>
///<reference path='Wall.ts'/>
///<reference path='Cat.ts'/>
///<reference path='Teacher.ts'/>
///<reference path='Student.ts'/>
///<reference path='Supervisor.ts'/>
module AnotherNamespace {
// Take note, since the other classes have a different module name we need to import them.
// If the classes have the same module name then you don't need to import.
import Brick = MyNamespace.Brick;
import Wall = MyNamespace.Wall;
import Cat = MyNamespace.Cat;
import Teacher = MyNamespace.Teacher;
import Student = MyNamespace.Student;
import Supervisor = MyNamespace.Supervisor;
export class TestApp {
constructor() {
console.log("!!!!!!!!!!!! Class Example");
var firstBrick:Brick = new Brick();
console.log("!!!!!!!!!!!! Inheritance Example");
var cat:Cat = new Cat();
console.log("!!!!!!!!!!!! Composition Example");
var myWall:Wall = new Wall(4,4);
console.log("!!!!!!!!!!!! Polymorphism Example");
var supervisor1:Supervisor = new Supervisor(new Teacher());
var supervisor2:Supervisor = new Supervisor(new Student());
}
}
}
///<reference path='Brick.ts'/>
module MyNamespace {
export class Wall {
public wallWidth:number;
public wallHeight:number;
constructor(w:number, h:number) {
this.wallWidth = w;
this.wallHeight = h;
this.build();
}
public build():void {
for(var i:number = 0; i < this.wallHeight; i++) {
for(var j:number = 0; j < this.wallWidth; j++) {
var brick:Brick = new Brick();
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.