JS 類別 – Let’s go.
句型 | 例子: |
class (父類別名稱){ //父類別內容 } | class Person{ constructor(name, surname){ this.name = name; this.surname = surname; } GetFullName(){ return this.name + ” ” + this.surname; } } |
句型 | 例子: |
class (類別名稱) : (抽象類別名稱){ //類別內容 //可引用父類別內容 //在例子中: string GetFullName } | class Student extends Person { constructor(name, surname, chiGrade , engGrade, mathsGrade) { super(name, surname) this.chiGrade = chiGrade; this.engGrade = engGrade; this.mathsGrade = mathsGrade; } isChiPassed() { if (this.chiGrade < 50) { return false; } return true; } //Omit …. } |
class (類別名稱) : (抽象類別名稱){ //類別內容 //可引用父類別內容 //在例子中: string GetFullName } | class Teacher extends Person{ constructor(name, surname, schoolName , teachingGrade) { super(name, surname) this.schoolName = schoolName; this.teachingGrade = teachingGrade; } isTeachingPassed() { if (chiGrade < 50) { return false; } return true; } } |
句型 | 例子: |
class (介面名稱){ //介面內容 } | class ICheckedPassed { isPassed (){ } } |
句型 | 例子: |
class (類別名稱) : (介面名稱){ //類別內容 //必須展開介面內容 //在例子中: bool isPassed() } | class Student extends (Person, ICheckedPassed){ constructor(chiGrade, engGrade, mathsGrade) { this.chiGrade = chiGrade; this.engGrade = engGrade; this.mathsGrade = mathsGrade; } isChiPassed(){ return this.chiGrade > 60; } isEngPassed(){ return this.engGrade > 50; } isMathsPassed(){ return this.mathsGrade > 55; } isPassed(){ return isChiPassed() && isEngPassed() && isMathsPassed(); } } |
句型 | 例子: |
class (類別名稱) : (介面名稱){ //類別內容 //必須展開介面內容 //在例子中: bool isPassed() } | class Teacher extends (Person, ICheckedPassed){ constructor(teachingGrade) { this.teachingGrade = teachingGrade; } isTeachingPassed(){ return this.teachingGrade > 45; } isPassed(){ return isTeachingPassed(); } } |
相關頁面:
JS介面 (類作模仿) – Interface 介面 的基本用法 – 加入 for loop, if, Array, Collection的例子
JS 繼承 – Inheritance 繼承 的基本用法 – 加入 for loop, if, Array, Collection的例子
參考資料: