fe-interview icon indicating copy to clipboard operation
fe-interview copied to clipboard

[TypeScript] 第1548天 在TypeScript中控制成员可见性有哪些方法?

Open haizhilin2013 opened this issue 2 years ago • 1 comments
trafficstars

第1548天 在TypeScript中控制成员可见性有哪些方法?

3+1官网

我也要出题

haizhilin2013 avatar Jul 11 '23 20:07 haizhilin2013

在 TypeScript 中,可以通过访问修饰符(access modifiers)来控制类成员的可见性。访问修饰符用于定义类成员(属性和方法)的访问级别,TypeScript 提供了以下几种访问修饰符:

  1. public(默认)

    • public 成员可以在任何地方访问,包括类的内部和外部。
    • 如果不指定访问修饰符,成员默认为 public
    class Animal {
        public name: string;
    
        public constructor(name: string) {
            this.name = name;
        }
    
        public move(distance: number) {
            console.log(`${this.name} moved ${distance} meters.`);
        }
    }
    
    const cat = new Animal('Cat');
    cat.name = 'Kitty'; // 访问公有属性
    cat.move(10);       // 调用公有方法
    
  2. private

    • private 成员只能在类的内部访问,不能在类的外部访问。
    class Animal {
        private name: string;
    
        public constructor(name: string) {
            this.name = name;
        }
    
        public move(distance: number) {
            console.log(`${this.name} moved ${distance} meters.`);
        }
    
        private makeSound() {
            console.log('Animal makes a sound');
        }
    }
    
    const cat = new Animal('Cat');
    // cat.name = 'Kitty'; // 错误:不能在类的外部访问私有属性
    // cat.makeSound();    // 错误:不能在类的外部访问私有方法
    
  3. protected

    • protected 成员只能在类的内部和继承该类的子类中访问,不能在类的外部访问。
    class Animal {
        protected name: string;
    
        public constructor(name: string) {
            this.name = name;
        }
    
        protected move(distance: number) {
            console.log(`${this.name} moved ${distance} meters.`);
        }
    }
    
    class Bird extends Animal {
        public fly(distance: number) {
            this.move(distance); // 可以在子类中访问受保护的方法
            console.log(`${this.name} flew ${distance} meters.`);
        }
    }
    
    const bird = new Bird('Sparrow');
    // bird.name = 'Robin'; // 错误:不能在类的外部访问受保护属性
    bird.fly(100);         // 正确:在子类中调用受保护方法
    
  4. readonly

    • readonly 成员只能在声明时或构造函数中赋值,之后不能修改。可以与其他访问修饰符组合使用。
    class Animal {
        public readonly name: string;
    
        public constructor(name: string) {
            this.name = name;
        }
    
        public move(distance: number) {
            console.log(`${this.name} moved ${distance} meters.`);
        }
    }
    
    const cat = new Animal('Cat');
    // cat.name = 'Kitty'; // 错误:不能修改只读属性
    cat.move(10);       // 调用公有方法
    

参数属性

TypeScript 允许在构造函数参数中使用访问修饰符,这样可以简化类成员的定义和初始化。

class Animal {
    constructor(public name: string, private age: number) {}

    public getAge() {
        return this.age;
    }
}

const cat = new Animal('Cat', 2);
console.log(cat.name); // 输出: Cat
console.log(cat.getAge()); // 输出: 2
// console.log(cat.age); // 错误:不能在类的外部访问私有属性

总结

在 TypeScript 中,可以通过 publicprivateprotected 访问修饰符来控制类成员的可见性,readonly 修饰符可以用来声明只读属性。使用这些修饰符,可以更好地封装和保护类的内部状态和行为,使代码更加健壮和易于维护。

llccing avatar Jun 26 '24 07:06 llccing