笔记。
- 基础
- 继承
- 修饰符
- readonly修饰符
- 参数属性
- 静态属性
- 可选类属性
- 存取器
- 抽象类
- 实例类型
- 对前面的知识补充
// 基础
// 继承
// 修饰符
// readonly修饰符
// 参数属性
// 静态属性
// 可选类属性
// 存取器
// 抽象类
// 实例类型
// 对前面的知识补充
// 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)
// 2.继承
class Parent {
name: string
constructor(name: string) {
this.name = name
}
}
class Child extends Parent {
constructor(name: string) {
super(name)
}
}
// 3.修饰符 public private protective
// public 公共 public声明的属性和方法在类的内部和外部均能访问到。
// private 私有 private声明的方法和属性只能在其类的内部访问。
// protected 受保护 protected声明的方法和属性只能在类的内部和其子类能访问。
class Parent1 {
private age: number
constructor(age: number) {
this.age = age
}
}
const p = new Parent1(13)
console.log(p)
// console.log(p.age) 报错,私有属性
// console.log(Parent1.age) Parent1上不存在属性age
class Child1 extends Parent1 {
constructor(age: number) {
super(age)
// console.log(super.age) 报错 继承的子类也无法访问私有属性
}
}
// protected受保护
class Parent2 {
protected age: number
constructor(age: number) {
this.age = age
}
protected getAge() {
return this.age
}
}
const p2 = new Parent2(13)
console.log(p2)
class Child2 extends Parent2 {
constructor(age: number) {
super(age)
console.log(super.getAge())
// console.log(super.age) 报错 继承的子类也无法访问私有属性
}
}
const c2 = new Child2(19)
// 4.readonly修饰符
class UserInfo {
readonly name: string
constructor(name: string) {
this.name = name
}
}
const userINfo = new UserInfo('v')
console.log(userINfo)
// userINfo.name = 'dan' 只读属性 不能修改
// 5.参数属性 constructor()参数前面加上修饰符
class T1 {
constructor(public name: string){
// todo
}
}
const t1 = new T1('nn')
console.log(t1)
// 6.静态属性
class Parent3 {
public static age: number = 18
public static getAge() {
return Parent3.age
}
constructor() {}
}
const p3 = new Parent3()
// console.log(p.age) 报错
console.log(Parent3.age)
// 7.可选类属性 ?标记
class Info {
public name: string
public age?: number
constructor(name: string, age?: number, public sex?: string) {
this.name = name
this.age = age
}
}
const info1 = new Info('vv')
const info3 = new Info('vvv', 18)
const info4 = new Info('vvv', 18, 'man')
console.log(info1)
console.log(info3)
console.log(info4)
// 8.存取器
class Info2 {
public name: string
public age?: number
private _infoStr: string
constructor(name: string, age?: number, public sex?: string) {
this.name = name
this.age = age
}
get infoStr() { // 取值器
return this._infoStr
}
set infoStr(val) { // 存值器
console.log(`setter: ${val}`)
this._infoStr = val
}
}
const info5 = new Info2('v', 22, 'v')
info5.infoStr = 'haha'
console.log(info5.infoStr)
// 9. 抽象类 abstract
abstract class People {
constructor(public name: string) {}
public abstract printName(): void
}
// const p1 = new People() 不能 直接用来创建实例,只能通过类继承
class Man extends People {
constructor(name: string) {
super(name)
this.name = name
}
printName() {
console.log(this.name)
}
}
const man = new Man('www')
man.printName()
abstract class PP { // 抽象方法和抽象存取器 都不能包含实际的代码
public abstract _name: string
abstract get insideName(): string
abstract set insideName(value: string)
}
class P extends PP {
public _name: string
insideName: string
}
// 10. 实例类型 类既是值也是类型
class PPP {
constructor(public name: string) {}
}
const p4 = new PPP('dddd')
// 11. 对前面的知识补充
// 类类型接口
interface FoodInterface {
type: string
}
class FoodClass1 implements FoodInterface {
public type: string
}
class A1 {
protected name: string // 受保护的属性只能在继承此类的子类中访问
}
interface I extends A1 {} // no-empty-interface
class B1 implements I {
public name: string // 无法使用name
}
// 需要这样写
class C1 extends A1 implements I {
public name: string
}
// 例
const create = <T>(c: new() => T): T => {
return new c()
}
class Infos {
public age: number
}
create(Infos)