typscript的面向对象以及多态重载说明

本文介绍了TypeScript中的类与对象概念,包括类定义、实例化、继承、访问修饰符(public、private、protected)、接口以及多态和重载的使用。通过实例展示了如何创建类、继承关系、访问私有属性以及利用接口和重载实现灵活的方法调用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

类与对象:

使用 class 关键字来定义类,通过 new 关键字实例化对象。

class Person {
    name: string;
    constructor(name: string) {
        this.name = name;
    }
    sayHello() {
        console.log(`Hello, ${this.name}!`);
    }
}
let person1 = new Person("Alice");
person1.sayHello(); // 输出:Hello, Alice!

继承:

使用 extends 关键字实现继承。

class Student extends Person {
    studentId: number;
    constructor(name: string, studentId: number) {
        super(name);
        this.studentId = studentId;
    }
    study() {
        console.log(`${this.name} is studying with student ID ${this.studentId}.`);
    }
}

let student1 = new Student("Bob", 12345);
student1.sayHello(); // 输出:Hello, Bob!
student1.study();    // 输出:Bob is studying with student ID 12345.

访问修饰符:

TypeScript 提供了 public、private 和 protected 等访问修饰符。

class Animal {
    private name: string;
    constructor(name: string) {
        this.name = name;
    }
    makeSound() {
        console.log(`${this.name} makes a sound.`);
    }
}
class Dog extends Animal {
    bark() {
        console.log(`${this.name} barks.`);
    }
}

let dog1 = new Dog("Buddy");
dog1.makeSound(); // 编译错误,name 是 private 属性,不能在子类外部访问
dog1.bark();      // 输出:Buddy barks.

接口:

使用 interface 关键字定义接口,类可以实现接口。

interface Shape {
    calculateArea(): number;
}
class Circle implements Shape {
    radius: number;
    constructor(radius: number) {
        this.radius = radius;
    }
    calculateArea() {
        return Math.PI * this.radius ** 2;
    }
}

let circle1 = new Circle(5);
console.log(circle1.calculateArea()); // 输出:78.54

在 TypeScript 中,多态(Polymorphism)和重载(Overloading)是面向对象编程中常见的两个概念。以下是它们的说明:
多态(Polymorphism):
多态是指对象可以根据其所属的类型表现出不同的行为。在 TypeScript 中,多态通常通过继承和方法重写来实现。

class Animal {
    speak() {
        console.log("Animal makes a sound");
    }
}
class Dog extends Animal {
    speak() {
        console.log("Dog barks");
    }
}
class Cat extends Animal {
    speak() {
        console.log("Cat meows");
    }
}
function makeAnimalSpeak(animal: Animal) {
    animal.speak();
}
let dog = new Dog();
let cat = new Cat();
makeAnimalSpeak(dog); // 输出:Dog barks
makeAnimalSpeak(cat); // 输出:Cat meows

在上面的例子中,makeAnimalSpeak 函数接受一个 Animal 类型的参数,但它可以接受 Dog 或 Cat 类型的实例,并且会根据实际类型调用相应的 speak 方法。

重载(Overloading):

重载是指在一个类或函数中定义多个相同名称但参数列表不同的方法。在 TypeScript 中,函数重载是通过在函数声明中提供多个签名来实现的

function add(a: number, b: number): number;
function add(a: string, b: string): string;
function add(a: any, b: any): any {
    if (typeof a === 'number' && typeof b === 'number') {
        return a + b;
    } else if (typeof a === 'string' && typeof b === 'string') {
        return a.concat(b);
    } else {
        throw new Error('Invalid arguments');
    }
}

console.log(add(5, 10));      // 输出:15
console.log(add('Hello', ' World')); // 输出:Hello World

在上面的例子中,add 函数有两个重载的签名,分别处理数字和字符串的相加。根据传入的参数类型,TypeScript 编译器会选择正确的重载。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老大白菜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值