面向对象
向对象的三大特征 :封装 继承 多态
封装
封装:隐藏对象的属性对外只提供可以访问属性的方法
例子:
export class Pet {
// 访问修饰符 本类,本包,其他包,子类
// public 共有的 任何位置都可以访问
public id: number
// default默认
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
}
}
class Pets {
constructor(id: number, name: string, sex: string, helth: number, love: number) {
this.id = id
this.name = name
this.sex = sex
this.helth = helth
this.love = love
}
//1.私有化属性
private id: number
private name: string
private sex: string
private helth: number//健康值
private love: number//亲密度
// 2.提供一个共有的方法来访问属性 getter/setter
public getId(): number {
return this.id
}
public getName(): string {
return this.name
}
public getsex(): string {
return this.sex
}
public gethelth(): number {
return this.helth;
}
public getlove(): number {
return this.love
}
// set 设置/赋值
public setId(id: number) {
this.id = id
}
public setName(name: string) {
this.name = name
}
public setSex(sex: string) {
this.sex = sex
}
public setHelth(helth: number) {
this.helth = helth
}
public setLove(love: number) {
this.love = love
}
}
继承
继承 :子类继承父类属性和方法
父类
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 a() {
console.log(`私有方法`);
}
public bb() {
console.log('父类的公有的方法');
}
}
// 子类
class B extends A {
// 子类可以有自己的属性和方法
b1: number
b2: number
b3() {
console.log('b的函数');
console.log(this.a1);
console.log(this.a2);
// 子类无法继承父类的私有的属性和方法
// console.log(this.a3);
// this.aa();
this.bb
}
// 派生类的构造函数必须包含super调用
constructor(a1: number, a2: number, a3: number, b1: number, b2: number) {
// super调用父类的构造函数 必须写在构造函数的第一行
super(a1, a2, a3)
this.b1 = b1
this.b2 = b2
}
}
let ba: B = new B(1, 2, 3, 4, 5);
console.log(ba);
ba.bb()
注意:
1.继承的关键字 extends
2.子类继承父类 只能继承一个父类(单继承)
3.一个父类可以有很多个子类
4.object 类是所有类的父类 只要没有显示继承的类都默认继承object
5.子类必须调用父类的构造函数,构造函数必须在第一行
6.子类不能继承父类的私有属性和方法
7.在子类中调用父类的属性和方法需要使用this关键字
8.子类属性和父类属性同名,默认使用子类的属性
9.方法同名时:默认调用子类的方法,可以使用super调用父类方法
多态:
// 方法的重载 :在同一个类中,方法名相同,返回值和参数不同
class A {
a1(): void {
console.log('第一个');
}
// a1(num:string):string{
// return ''
// }
}
// 方法的重写: 子类重写父类的方法,方法名相同,参数相同返回值相同或者其子类
class B extends A {
a1(): void {
console.log('子类的重写');
}
}
let b :B=new B ()
b.a1()
有关多态的练习
在这里插入代码片// 宠物
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;//健康值+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}`);
}
show(): string {
let str: string = super.show()
return str + `,品种${this.type}`
}
jfp() {
console.log(`狗在接飞盘,健康值-10,亲密度+5`);
this.health -= 10
this.love += 5
}
}
// 企鹅
class Pengun extends Pet {
age: number
constructor(age: number, name: string, sex: string, health: number, love: number) {
super(name, sex, health, love)
this.age = age
}
eat(food: string): void {
if (this.health <= 95) {
this.health += 5
} else {
this.health = 100
}
console.log(`企鹅在吃:${food},健康值+5,当前健康值:${this.health}`);
}
show(): string {
let str: string = super.show()
return str + `,品种${this.age}`
}
yy() {
console.log(`企鹅在游泳,健康值-8,亲密度+3`);
this.health -= 8
this.love += 3
}
}
// 猫
class Cat extends Pet {
breed: string
constructor(breed: string, name: string, sex: string, health: number, love: number) {
super(name, sex, health, love)
this.breed = breed
}
eat(food: string): void {
if (this.health <= 95) {
this.health += 5
} else {
this.health = 100
}
console.log(`猫在吃:${food},健康值+5,当前健康值:${this.health}`);
}
show(): string {
let str: string = super.show()
return str + `,品种${this.breed}`
}
ws() {
console.log(`猫在玩逗猫棒,健康值-3,亲密度+3`);
this.health -= 3
this.love += 10
}
}
// 主人:领养宠物 喂养宠物
class Master {
// getDog(): Dog {
// let dog: Dog = new Dog('泰迪', '小小', '公', 90, 80)
// // console.log(`恭喜你领养了一个宠物狗:${dog}`);
// return dog
// }
// getPengun(): Pengun {
// let pengun: Pengun = new Pengun(3, '会会', '公', 90, 80)
// // console.log(`恭喜你领养了一个宠物狗:${pengun}`);
// return pengun
// }
// 以父类作为形参实现多态
getPet(p: Pet) {
console.log(p.show());
}
// 以父类形式形成多态
// 喂宠物
feedPet(p: Pet) {
// 判断p是不是一个狗 判断一个对象是不是某个类型
if (p instanceof Dog) {
p.eat('骨头')
} else if (p instanceof Pengun) {
p.eat('鱼')
} else if (p instanceof Cat) {
p.eat('猫粮')
}
}
//玩游戏
play(p: Pet) {
// as断言 了解
// let pg:Pengun=p as Pengun;
// pg.yy()
if (p instanceof Dog) {
// 前面已经做了类型的判断,所以系统会自动把对象转换成对应的类型
p.jfp()
} else if (p instanceof Pengun) {
p.yy()
} else if (p instanceof Cat) {
p.ws()
}
}
}
let m: Master = new Master();
let dd: Dog = new Dog('泰迪', '小小', '公', 80, 70)
let str = m.getPet(dd)
// m.feedPet(dd)
let pg: Pengun = new Pengun(2, '小小', '公', 80, 70)
m.feedPet(pg)
m.play(pg)
let mao:Cat=new Cat('蓝猫','小小','母',70,80)
m.feedPet(mao)
m.play(mao)
接口
// 接口:规定类必须具有的功能
interface A {
// 接口没有构造函数
num1: number
// 接口中的方法不能有方法体 ,必须写上返回值的类型
a1(): void;
}
// 接口不能被实例化
// let a :A=new A()
// 实现类:必须有接口的属性,必须重写接口中的方法
class B implements A {
num1: number;
a1(): void {
console.log('重写的接口的函数');
}
constructor(num1: number) {
this.num1 = num1
}
}
let bb: B = new B(1);
bb.a1()
练习
interface Usb {
in(): void;
outs(): void;
}
class Fs implements Usb {
type: string
ts() {
console.log('调节风扇速度');
}
constructor(type: string) {
this.type = type
}
in(): void {
console.log('风扇插入Usb');
}
outs(): void {
console.log('风扇拔出Usb');
}
}
let fss: Fs = new Fs('海尔')
fss.in()
fss.ts()
fss.outs()
class Sb implements Usb {
type: string
bs() {
console.log('滚动');
}
constructor(type: string) {
this.type = type
}
in(): void {
console.log('鼠标插入Usb');
}
outs(): void {
console.log('鼠标拔出Usb');
}
}
let ss: Sb = new Sb('联想')
ss.in()
ss.bs()
ss.outs()
// 容量 存数据 读数据
class Rl implements Usb {
rl: string
rr() {
console.log('容量为1000');
}
cs() {
console.log('存数据');
}
ds() {
console.log('读数据');
}
constructor(rl: string) {
this.rl = rl
}
in(): void {
console.log('u盘插入Usb');
}
outs(): void {
console.log('u盘拔出Usb');
}
}
let rr: Rl = new Rl('UP')
rr.in()
rr.rr()
rr.cs()
rr.ds()
rr.outs()