Modifiers for TypeScript classes

Posted by Maniacility on Sat, 31 Aug 2019 06:20:16 +0200

Modifiers for TypeScript classes

There are three modifiers available when defining attributes within typescript

  • public: public is accessible inside, outside, and subclasses of the current class
  • protected: protected types are accessible within the current class, within subclasses, and outside classes
  • private: private is accessible inside the current class, but not outside the subclass or class
  • Attributes are public by default without modifiers

public , protected, private

    class MyPerson{
        public name: string;
        protected age: number;
        private gender: string;
        constructor(name: string, age: number, gender: string){
            this.name = name;
            this.age = age;
            this.gender = gender;
        }
        nameMethods(): string{
            return `${this.name}In Sports`;
        }
        ageMethods(): string{
            return `Already ${this.age}`
        }
        genderMethods(): string{
            return `${this.gender}nature`
        }
    }

    class Person1 extends MyPerson{
        constructor(name: string, age: number, gender: string){
            super(name,age,gender);
        }
        personName(): string{
            return `${this.name}person1 At work`
        }
        personAge(): string{
            return `person1 Already ${this.age}`
        }
        personGender(): string{
            return `person1 time ${this.gender}`
        }
    }
    //Parent Class
    let person = new MyPerson('red',15,'woman');
    console.log(person.name);
    console.log(person.age);
    console.log(person.gender);
    //Subclass
    let person1 = new Person1('blue',18,'man');
    console.log(person1.personName());
    console.log(person1.personAge());
    console.log(person1.personGender());

Abstract classes in TypeScript:

It is a base class that provides inheritance from other classes and cannot be instantiated directly.

  • Define abstract classes and methods with abstract keywords.
  • An abstract method in an abstract class does not contain a concrete implementation and must be implemented in a derived class.
  • Abstract classes and methods are used to define standards.
  • Standard: Animal is a class that requires its subclasses to contain eat methods
abstract class Animal {
    constructor(public name: string){
        this.name = name;
    }
    eating(): void{
        console.log('department name:'+ this.name);
    }
    abstract barking(): void{
        
    }//Must be implemented in a derived class
}

class Dog extends Animal {
    constructor(){
        super('animal')
    }
    barking(): void{
        console.log('barking');
    }
    other(): void{
        console.log('other')
    }
}
//animal = new Animal()//error: cannot create instances of abstract classes
let animal: Animal = new Dog();   //Create a reference to an abstract type and instantiate and assign Abstract subclasses
animal.eating();
animal.barking();  
animal.other()  //Error.Method does not exist in declared abstract class


Class used as interface

A class definition creates two things: an instance type of the class and a constructor.Because classes can create types, you can use classes where interfaces are allowed.

Topics: TypeScript