Skip to content

Latest commit

 

History

History
317 lines (261 loc) · 5.23 KB

File metadata and controls

317 lines (261 loc) · 5.23 KB

juin 5 2022

面向对象概述

动物歌唱比赛

面向过程

把所有的功能都写到函数里,然后依次调用

function dogShout(){
    console.log('wang')
}
function birdShout(){
    console.log('ying')
}
function catShout(){
    console.log('miao')
}

catShout()
dogShout()
birdShout()

面向对象

是吧功能写到类里面,这里我应该写三个类,猫,狗,鸟

唱歌(功能)是类的方法

Class Cat {
    shout(){
        console.log('miao')
    }
}

Class Dog {
    shout(){
        console.log('wang')
    }
}

Class Bird {
    shout(){
        console.log('ying')
    }
}

// 要调用方法,就要去实例化这个类
let cat:Cat = new Cat()
let dog:Dog = new Dog()

cat.shout()
dog.shout()
bird.shout()

代码重要的地方

  1. 健壮性

  2. 可维护性

  3. 可扩展性

  4. 可读性\

封装

将类中的属性设置为私有属性,通过getter和setter来获取和更改,而不是直接打点获取这个数据

class Cat {
    // 将属性私有化  如果不私有化,可以在外部获得属性,并修改属性
	private age:number
    shout(){
        console.log('miao')
    }
    // 设置属性
    // getter
    getAge(){
        return this.age
    }
    // setter
    setAge(age:number){
        if(age >= 0 && age < 20){
            this.age = age	
        }else{
            console.log('设置年龄失败')
            this.age = 0
        }
    }
}

let cat:Cat = new Cat()
// cat.age = 5  不是私有的就可以在外部直接访问和设置
// console.log(cat.age)

cat.setAge(5)
console.log(cat.getAge)

继承

class Dog {
    // 将属性私有化  如果不私有化,可以在外部获得属性,并修改属性
	private age:number
    shout(){
        console.log('wang')
    }
    // 设置属性
    // getter
    getAge(){
        return this.age
    }
    // setter
    setAge(age:number){
        if(age >= 0 && age < 20){
            this.age = age	
        }else{
            console.log('设置年龄失败')
            this.age = 0
        }
    }
}

class Bird {
    // 将属性私有化  如果不私有化,可以在外部获得属性,并修改属性
	private age:number
    shout(){
        console.log('ying')
    }
    // 设置属性
    // getter
    getAge(){
        return this.age
    }
    // setter
    setAge(age:number){
        if(age >= 0 && age < 20){
            this.age = age	
        }else{
            console.log('设置年龄失败')
            this.age = 0
        }
    }
}

let cat:Cat = new Cat()
// cat.age = 5  不是私有的就可以在外部直接访问和设置
// console.log(cat.age)

cat.setAge(5)
console.log(cat.getAge)

let dog:Dog = new Dog()
let bird:Bird = new Bird()
cat.shout()
dog.shout()
bird.shout()

上面的例子有大量重复,用继承进行重构,提取公共特征

Class Animal {
    private age:number
     // 设置属性
    // getter
    getAge(){
        return this.age
    }
    // setter
    setAge(age:number){
        if(age >= 0 && age < 20){
            this.age = age	
        }else{
            console.log('设置年龄失败')
            this.age = 0
        }
    }
    shout(){}
}

class Dog extends Animal {
    // 继承并重写了父类的方法
    shout(){
        console.log('wang')
    }
}

class Cat extends Animal {
    shout(){
        console.log('miao')
    }
}

class Bird extends Animal {
    shout(){
        console.log('ying')
    }
    fly(){
        console.log('fly')
    }
}

cat.setAge(5)
console.log(cat.getAge)

多态

定义一个类,来管理游戏

class Game {
	catShout(player:Cat){
        player.shout()
    }
    dogShout(player:Dog){
        player.shout()
    }
    birdShout(player:Bird){
        player.shout()
    }
}

let dog:Dog = new Dog()
let bird:Bird = new Bird()
let game:Game = new Game()
game.catShout(cat)
game.dogShout(dog)
game.birdShout(bird)

上面的例子shout很类似,重构一下

class Game {
	playShout(player:Animal){
        player.shout()
    }
}
// 多态的特性,可以向上转型
// cat向上转型为Animal,但是调用方法仍是cat的方法
// 也可以理解为cat有多种状态,Animal和Cat状态
let dog:Dog = new Dog()
let bird:Bird = new Bird()
let game:Game = new Game()
game.playShout(cat)
game.playShout(dog)
game.playShout(bird)

多态的应用

修改刚刚的游戏

新加的参赛选手

interface 可以描述一个对象的结构

// 找到公共的方法,然后把公共的方法定义为接口
// 通过接口定义类型
interface ToSky {
    fly():void
}
class Plane implements ToSky{
    fly(){
        console.log('plane fly')
    }
}

class Bird extends Animal implements ToSky{
    shout(){
        console.log('ying')
    }
    fly(){
        console.log('bird fly')
    }
}
class Game {
	playShout(player:Animal){
        player.shout()
    }
    playFly(player:ToSky){
    // 猫狗不会飞 只有鸟有飞的方法 飞机又不是动物,所以不能 palyer:Animal
        player.fly()
    }
}

let dog:Dog = new Dog()
let bird:Bird = new Bird()
let game:Game = new Game()
let plane:Plane = new Plane()

game.playFly(bird)
game.playFly(plane)

泛型

使用泛型,具有灵活性和类型安全性,只要满足限制条件,都可以输入