筆記。未完待續。
1.基礎 在ts中定義類
class Point {
x: number
y: number
constructor(x: number, y: number) {
this.x = x
this.y = y
}
getPosition() {
return `(${this.x}, ${this.y})`
}
}
const poit = new Point(1, 3)
console.log(poit)
// 繼承
class Parent {
name: string
constructor(name: string) {
this.name = name
}
}
class Child extends Parent {
constructor(name: string) {
super(name)
}
}
2.修飾符 public private protective
// public 公共 (不寫 默認)
// private 私有
class Parent1 {
private age: number
constructor(age: number) {
this.age = age
}
}
const p = new Parent1(13)
console.log(p)
// console.log(Parent1.age)console.log(p.age) 報錯,私有屬性
// console.log(Parent1.age) Parent1上不存在屬性age
class Child1 extends Parent1 {
constructor(age: number) {
super(age)
// console.log(super.age) 報錯 繼承的子類也無法訪問私有屬性
}
}
// protected受保護