Java 類別 – Let’s Start!
句型 | 例子: |
interface (父類別名稱){ //父類別內容 } | class Person{ public String name; public String surname; public Person(String name, String surname){ this.name = name; this.surname = surname; }; public String GetFullName(){ return this.name + ” ” + this.surname; }; } |
句型 | 例子: |
class (類別名稱) extends (抽象類別名稱){ //類別內容 //可引用父類別內容 //在例子中: string GetFullName } | class Student extends Person { public int chiGrade; public int engGrade; public int mathsGrade; public Student(String name, String surname, int chiGrade , int engGrade, int mathsGrade) { super(name, surname); this.chiGrade = chiGrade; this.engGrade = engGrade; this.mathsGrade = mathsGrade; } public Boolean isChiPassed() { if (chiGrade < 50) { return false; } return true; } //Omit …. } |
class (類別名稱) extends (抽象類別名稱){ //類別內容 //可引用父類別內容 //在例子中: string GetFullName } | class Teacher extends Person{ public int teachingGrade; public String schoolName; public Teacher(String name, String surname, String schoolName, int teachingGrade) { super(name, surname) ; this.schoolName = schoolName; this.teachingGrade = teachingGrade; } public Boolean isTeachingPassed() { if (this.teachingGrade< 50) { return false; } return true; } } |
句型 | 例子: |
interface (介面名稱){ //介面內容 } | interface ICheckedPassed{ Boolean isPassed(); } |
句型 | 例子: |
class (類別名稱) implements (介面名稱){ //類別內容 //必須展開介面內容 //在例子中: bool isPassed() } | class StudentMart extends Student implements ICheckedPassed{ public int chiGrade; public int engGrade; public int mathsGrade; public Boolean isPassed(){ return isChiPassed() && isEngPassed() && isMathsPassed(); } //Omit …. } |
句型 | 例子: |
class (類別名稱) implements (介面名稱){ //類別內容 //必須展開介面內容 //在例子中: bool isPassed() } | class TeacherMark extends Teacher implements ICheckedPassed { public int teachingGrade; public String schoolName; public Boolean isPassed(){ return isTeachingPassed(); } //Omit …. } |
相關頁面:
Java 介面 – Interface 的基本用法 – 結合 for loop, if, Array, Collection作例子
Java 繼承 – Inheritance 繼承 的基本用法 – 結合 for loop, if, Array, Collection作例子
參考網頁: https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/lang/Class.html