-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.ts
146 lines (113 loc) · 2.89 KB
/
app.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
143
144
145
146
//import {PI, calculateCircumference} from './math/circle';
//import {calculateRectangle} from './math/rectangle';
/*class Person{
name: string;
private type: string;
protected age: number = 32;
constructor(name: string, public username: string){
this.name = name;
}
printAge(){
console.log(this.age);
this.setType("Old Guy")
}
private setType(type: string){
this.type = type;
console.log(this.type);
}
}
const person = new Person("Shawna", "shawleigh17");
console.log(person);
console.log(person.name, person.username);
person.printAge();
//person.setType("Cool guy"); //Won't work with Private method
class Shawna extends Person{
constructor(username: string) {
super("Shawna", username);
this.age = 29;
}
}
const shawna = new Shawna("shawleigh17");
console.log(shawna);
//Getters and Setters
class Plant{
private _species: string = "Default";
get species(){
return this._species;
}
set species(value: string){
if(value.length > 3){
this._species = value;
}
else{
this._species = "Default";
}
}
}
let plant = new Plant();
console.log("Species: " + plant.species);
plant.species = "ABCC";
console.log("Species: " + plant.species);
//Static Properties and Methods
class Helpers{
static PI: number = 3.14;
static calcCircumference(diameter: number): number{
return this.PI * diameter;
}
}
console.log(2*Helpers.PI);
console.log(Helpers.calcCircumference(8));
abstract class Project{
projectName: string = "Default";
budget: number = 1000;
abstract changeName(name: string): void;
calcBudget(){
return this.budget * 2;
}
}
class ITProject extends Project{
changeName(name: string): void{
this.projectName = name;
}
}
let newProject = new ITProject();
console.log(newProject);
newProject.budget = 3000;
newProject.changeName("Super IT Project");
console.log(newProject);
class OnlyOne{
private static instance: OnlyOne;
public readonly name3: string;
private constructor(public name: string) {
this.name3 = name;
}
static getInstance(){
if(!OnlyOne.instance){
OnlyOne.instance = new OnlyOne("The Only One");
}
return OnlyOne.instance;
}
}
//let wrong = new OnlyOne("The Only One");
let right = OnlyOne.getInstance();
console.log(right.name3);
//right.name = "Something else";
//console.log(right.name);
console.log(calculateRectangle(10,20));
console.log(calculateCircumference(10));
console.log(PI);
*/
function hello(person: {name: string}){
console.log("Hello2, ") + person.name;
}
function changeName(person: {name: string}){
person.name = "Anna";
console.log("Hiya! " + person.name);
}
const person = {
name: "Shawna",
age: 32
};
hello(person);
changeName(person);
hello(person);