接口
接口:规定类必须具有的功能
接口没有构造函数(constructor)
接口中的方法不能有方法体,必须写上返回值类型
接口不能被实例化 访问不了
interface A {
num1: number
a1 ( ) : void ;
}
实现类:必须有接口中的属性,必须重写接口中的方法 B实现了A
class B implements A {
num1: number ;
constructor ( num1: number ) {
this . num1= num1
}
a1 ( ) : void {
console . log ( '重写的接口的函数' ) ;
}
}
let bb: B = new B ( 1 ) ;
bb. a1 ( )
面向对象
面向对象的三大特征:封装 继承 多态
封装
封装:隐藏对象的属性对外只提供可以访问属性的方法
访问修饰符 本类,本包,其他包,子类
export导出关键词(在其他页面导入)
public 共有的 任何位置都可以访问
default 默认
private私有的 只允许本类访问
export class Pet {
public id: number
name: string
private sex: string
b ( ) {
console . log ( '默认' ) ;
}
private a ( ) {
console . log ( '私有' ) ;
}
constructor ( id: number , name: string , sex: string ) {
this . id = id
this . name = name
this . sex = sex
}
}
属性“sex/a”为私有属性,只能在类“Pet”中访问。p.a() console.log(p.sex)
import { Pet } from "../bbb/extends" ;
let p: Pet= new Pet ( 1 , '1' , '1' )
console . log ( p. name) ;
p. b ( )
提供一个共有的方法来访问属性 getter/setter
继承
继承 子类继承父类的属性和方法
class A {
public a1: number
a2: number
private a3: number
constructor ( a1: number , a2: number , a3: number ) {
this . a1 = a1
this . a2 = a2
this . a3 = a3
}
public getA3 ( ) : number {
return this . a3
}
public setA3 ( a3: number ) { this . a3 = a3 }
private aa ( ) {
console . log ( '私有方法' ) ;
}
public bb ( ) {
console . log ( '父类的公有方法' ) ;
}
}
继承的关键字 extends
子类可以有自己的属性和方法
子类无法继承父类的私有数学和方法
派生类的构造函数必须包含’super’调用
super :调用父类的构造函数 必须写在构造函数的第一行
class B extends A {
b1: number
b2: number
b3 ( ) {
console . log ( 'b的函数' ) ;
console . log ( this . a1) ;
console . log ( this . a2) ;
this . bb ( )
}
constructor ( a1: number , a2: number , a3: number , b1: number , b2: number ) {
super ( a1, a2, a3)
this . b1 = b1
this . b2 = b2
}
}
let ba: B = new B ( 1 , 2 , 3 , 4 , 5 ) ;
ba. bb ( )
console . log ( ba) ;
总结:
1.继承的关键字 extends
class B extends A { }
2.子类继承父类 只能继承一个类(单继承)
class B extends A { }
3.一个父类 可以有多个子类
class B extends A { }
class C extends A { }
4.object类是所有类的父类,没有显式继承的类都默认继承object
5.子类必须调用父类的构造函数,构造函数必须在第一行
constructor ( a1: number , a2: number , a3: number , b1: number , b2: number ) {
super ( a1, a2, a3)
this . b1 = b1
this . b2 = b2
}
6.子类不能继承父类的私有属性和方法
b3 ( ) {
console . log ( 'b的函数' ) ;
console . log ( this . a1) ;
console . log ( this . a2) ;
this . bb ( )
}
7.在子类中调用父类的属性和方法需要使用this关键字
8.子类属性和父类属性同名,默认使用子类的属性
class Person {
constructor ( name: string , sex: string ) {
this . name = name
this . sex = sex
}
name: string
sex: string
eat ( ) {
console . log ( '人的吃喝拉撒' ) ;
}
}
class GoodPerson extends Person {
color: string
sex: string ;
constructor ( name: string , sex: string , color: string ) {
super ( name, sex)
this . color= color
this . sex= sex
}
9.方法可以同名,默认调用子类方法,可以使用super调用父类方法
eat ( ) {
super . eat ( )
console . log ( '好人的吃喝拉撒' ) ;
}
多态
as 断言 我觉得这是个什么
重写:(eat)
class Pet {
name: string
sex: string
health: number
love: number
constructor ( name: string , sex: string , health: number , love: number ) {
this . name= name
this . sex= sex
this . health= health
this . love= love
}
eat ( food: string ) {
console . log ( '宠物在吃' + food+ ',健康值加1' ) ;
this . health+= 1 ;
}
show ( ) : string {
return ` 昵称: ${ this . name} ,性别: ${ this . sex} ,健康值: ${ this . health} ,亲密度: ${ this . love} `
}
}
class Dog extends Pet {
type: string
constructor ( type: string , name: string , sex: string , health: number , love: number ) {
super ( name, sex, health, love)
this . type= type
}
eat ( food: string ) : void {
if ( this . health<= 97 ) {
this . health+= 3
} else {
this . health= 100
}
console . log ( ` 狗在吃 ${ food} ,健康值+3,,当前健康值: ${ this . health} ` ) ;
}
}
以父类作为形参实现多态
class Pet {
name: string
sex: string
health: number
love: number
constructor ( name: string , sex: string , health: number , love: number ) {
this . name= name
this . sex= sex
this . health= health
this . love= love
}
eat ( food: string ) {
console . log ( '宠物在吃' + food+ ',健康值加1' ) ;
this . health+= 1 ;
}
show ( ) : string {
return ` 昵称: ${ this . name} ,性别: ${ this . sex} ,健康值: ${ this . health} ,亲密度: ${ this . love} `
}
}
class Master {
getPet ( p: Pet) {
console . log ( p. show ( ) ) ;
}
feedPet ( p: Pet) {
if ( p instanceof Dog ) {
p. eat ( '骨头' )
} else if ( p instanceof Pengun ) {
p. eat ( '鱼' )
} else if ( p instanceof Cat ) {
p. eat ( '猫粮' )
}
}
play ( p: Pet) {
if ( p instanceof Dog ) {
p. jfp ( )
} else if ( p instanceof Pengun ) {
p. yy ( )
} else if ( p instanceof Cat ) {
p. mm ( )
}
}
}
ResourceStr 资源字符串/文件
@ State img: ResourceStr= $rawfile ( '2024-08-28_144259.png' )