1. 面向对象之class(一)
1.1 class 与 interface 的区别
interface PointInterface {
x: number
y: number
}
class PointClass {
x: number
y: number
}
const p = new PointClass()
p.x = 1
p.y = 1
1.2 class之构造函数
class Point {
x!: number
y!: number
constructor(x: number, y: number)
constructor(s: string)
constructor(xs: number | string, y?: number) {
if (typeof xs === 'number' && typeof y === 'number') {
this.x = xs
this.y = y
} else if (typeof xs === 'string') {
const parts = xs.split(',')
this.x = parseFloat(parts[0])
this.y = parseFloat(parts[1])
}
}
}
const p = new Point('1, 2')
console.log(p.x, p.y)
class Hash {
[s: string]: unknown
set(key: string, value: unknown) {
this[key] = value
}
get(key: string) {
return this[key]
}
}
1.3 class可以实现接口继承
interface Person {
name: string
age?: number
sayHi: (target: Person) => void
}
interface Taggable {
tags: string[]
addTag: (tag: string) => void
removeTag: (tag: string) => void
}
class User implements Person, Taggable {
name: string
tags: string[] = []
addTag(tag: string) {
this.tags.push(tag)
}
removeTag(tag: string) {
const index = this.tags.indexOf(tag)
this.tags.splice(index, 1)
}
sayHi(target: Person) {
console.log(`Hi ${target.name}`)
}
}
1.4 class能继承class
/* class Person {
constructor(public name: string) {}
sayHi() {
console.log(`你好, 我是${this.name}`)
}
}
// 没有重写
// class User extends Person {
// constructor(public id: number, name: string) {
// super(name)
// }
// login() {}
// }
class User extends Person {
constructor(public id: number, name: string) {
super(name)
}
login() {}
// 重写(overwrite) 是可以有机会调用父类的方法
// 重载 (overload) 类型不同/参数个数不同/两个都不同 需要实现不同的方法
// 重写方法
sayHi(target?: User) {
if (target === undefined) {
super.sayHi()
} else {
console.log(` ${target.name} ${this.name}`)
}
}
}
const u = new User(1,
u.sayHi()
u.login() */
class Person {
friend?: Person
constructor(public name: string, friend?: Person) {
this.friend = friend
}
}
class User extends Person {
// 重写属性 关键字 declare
// 如果不加, 会发生错误, u2.friend 类型是一个 Person
declare friend: User
constructor(public id: number, name: string, friend?: User) {
super(name, friend)
}
}
const u1 = new User(1,
const u2 = new User(1,
u2.friend
2. 面向对象之class(二)
2.1 四种成员可见性
class Person {
#name: string
constructor(public name: string) {
this.#name = name
}
}
2.2 static属性和static block
class Person {
static counter: number
name: string
constructor(name: string) {
this.name = name
}
}
const p = new Person('lwz')
p.name
Person.counter
class Foo {
static #count = 0
static {
const count = loadFromLocalStorage() || 0
Foo.#count += count
}
constructor() {
console.log(Foo.#count)
}
}
2.3 类和泛型
class Hash<K, V> extends Map<K, V> {
destroy() {
this.clear()
}
}
2.4 抽象类
const Rectangle = class {
constructor(public height: number, public width: number) {}
area() {
return this.height * this.width
}
}
const r = new Rectangle(100, 200)
console.log(r.area())
interface A {
name: string
age: number
}
class B {
name: string
age: number
constructor(name: string, age: number) {
this.name = name
this.age = age
}
}
abstract class C {
abstract name: string
age: number
constructor(age: number) {
this.age = age
}
abstract a(): number
abstract b(): number
}
class D extends C {
name: string
constructor(name: string) {
super(18)
this.name = name
}
a() {
return 1
}
b() {
return 2
}
}
const c = new D('lwz')
2.5 把类当成参数
class Person {
constructor(public name: string) {}
}
function f(X: typeof Person) {
const p = new X('frank')
console.log(p.name)
}
f(Person)
function ff(x: string) {}
ff('hello')
function f2(X: new (name: string) => Person) {
const p = new X('frank')
console.log(p.name)
}
f2(Person)
function ff2(x: (name: string) => string) {}
ff2((name) => 'hello')