Skip to content

Instantly share code, notes, and snippets.

@deyvicode
Last active May 19, 2023 04:48
Show Gist options
  • Save deyvicode/37fad6bc2f808ac94ce44cbb46960650 to your computer and use it in GitHub Desktop.
Save deyvicode/37fad6bc2f808ac94ce44cbb46960650 to your computer and use it in GitHub Desktop.
TypeScript - POO
class Course {
constructor(private _id: number, private _name: string) {}
// get and set
get id() {
return this._id
}
set id(id: number) {
this._id = id
}
get name() {
return this._name
}
set name(name: string) {
this._name = name
}
}
class School {
private courses: Course[] = []
constructor(private _name: string) {}
// get and set
get name() {
return this._name
}
set name(name: string) {
this._name = name
}
public addCouse(course: Course) {
this.courses.push(course)
}
}
const school = new School('MySchool')
let courseJS = new Course(1, 'JavaScript')
let courseTS = new Course(2, 'TypeScript')
school.addCouse(courseJS)
school.addCouse(courseTS)
courseJS.id = 10 // SET
courseJS.name = 'JavaScript Basic' // SET
console.log('school', school)
console.log('school.name', school.name) // GET
console.log('course.name', courseJS.name) // GET
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment