MST

星途 面试题库

面试题:TypeScript函数重载在复杂场景中的运用

假设有一个图形绘制库,需要实现一个 `draw` 函数,通过函数重载实现:传入 `Circle` 类型对象(包含 `radius` 属性)绘制圆形;传入 `Rectangle` 类型对象(包含 `width` 和 `height` 属性)绘制矩形;传入 `Triangle` 类型对象(包含 `side1`、`side2`、`side3` 属性)绘制三角形。请写出 `draw` 函数的重载声明及实现。
39.1万 热度难度
前端开发TypeScript

知识考点

AI 面试

面试题答案

一键面试

以下以Python为例,Python本身不支持传统意义上基于参数类型的函数重载,这里使用isinstance函数来模拟实现类似效果:

class Circle:
    def __init__(self, radius):
        self.radius = radius

class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height

class Triangle:
    def __init__(self, side1, side2, side3):
        self.side1 = side1
        self.side2 = side2
        self.side3 = side3

def draw(shape):
    if isinstance(shape, Circle):
        print(f"绘制圆形,半径为: {shape.radius}")
    elif isinstance(shape, Rectangle):
        print(f"绘制矩形,宽为: {shape.width},高为: {shape.height}")
    elif isinstance(shape, Triangle):
        print(f"绘制三角形,边长分别为: {shape.side1}, {shape.side2}, {shape.side3}")
    else:
        print("不支持的图形类型")

如果是Java语言实现如下:

class Circle {
    double radius;
    public Circle(double radius) {
        this.radius = radius;
    }
}

class Rectangle {
    double width;
    double height;
    public Rectangle(double width, double height) {
        this.width = width;
        this.height = height;
    }
}

class Triangle {
    double side1;
    double side2;
    double side3;
    public Triangle(double side1, double side2, double side3) {
        this.side1 = side1;
        this.side2 = side2;
        this.side3 = side3;
    }
}

class DrawUtil {
    public static void draw(Circle circle) {
        System.out.println("绘制圆形,半径为: " + circle.radius);
    }

    public static void draw(Rectangle rectangle) {
        System.out.println("绘制矩形,宽为: " + rectangle.width + ",高为: " + rectangle.height);
    }

    public static void draw(Triangle triangle) {
        System.out.println("绘制三角形,边长分别为: " + triangle.side1 + ", " + triangle.side2 + ", " + triangle.side3);
    }
}

测试代码:

public class Main {
    public static void main(String[] args) {
        Circle circle = new Circle(5);
        Rectangle rectangle = new Rectangle(4, 6);
        Triangle triangle = new Triangle(3, 4, 5);

        DrawUtil.draw(circle);
        DrawUtil.draw(rectangle);
        DrawUtil.draw(triangle);
    }
}

如果是C++语言实现如下:

#include <iostream>

class Circle {
public:
    double radius;
    Circle(double radius) : radius(radius) {}
};

class Rectangle {
public:
    double width;
    double height;
    Rectangle(double width, double height) : width(width), height(height) {}
};

class Triangle {
public:
    double side1;
    double side2;
    double side3;
    Triangle(double side1, double side2, double side3) : side1(side1), side2(side2), side3(side3) {}
};

void draw(const Circle& circle) {
    std::cout << "绘制圆形,半径为: " << circle.radius << std::endl;
}

void draw(const Rectangle& rectangle) {
    std::cout << "绘制矩形,宽为: " << rectangle.width << ",高为: " << rectangle.height << std::endl;
}

void draw(const Triangle& triangle) {
    std::cout << "绘制三角形,边长分别为: " << triangle.side1 << ", " << triangle.side2 << ", " << triangle.side3 << std::endl;
}

测试代码:

int main() {
    Circle circle(5);
    Rectangle rectangle(4, 6);
    Triangle triangle(3, 4, 5);

    draw(circle);
    draw(rectangle);
    draw(triangle);

    return 0;
}