1. class People {
    2. name: string;
    3. age: number;
    4. constructor(name: string, age: number) {
    5. this.name = name;
    6. this.age = age;
    7. }
    8. eat(food: string): void {
    9. console.log("eat", food);
    10. }
    11. }
    12. const p = new People("zhangsan", 18);
    13. p.eat("apple");

    定义了一个类,还会自动定义一个接口

    1. interface People {
    2. name: string;
    3. age: number;
    4. eat: (food: string) => void;
    5. }