This is the sixth day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

Examples of TypeScript

In this article: TypeScript class inheritance and access types.Copy the code
The definition of a class

Classes in TypeScript are very similar to classes native to JaveScript ES6, with more features added.

/ / 1
/ / define the class
class Person {
    name = 'bear';
    getName() {
        return this.name
    }
}
// Create an instance
const person = new Person();
Copy the code

Example 1 defines a class Person using the class syntax and defines both the class attribute name and the method getName(). Once you have a class, you can create instances from the class.

Class inheritance
/ / 2
class Student extends Person{
    study() {
        return 'study'}}const student = new Student();
Copy the code

Classes inherit through the extends syntax, inheriting from the parent class (Person in Example 2) and from the child class (Student in Example 2). Subclasses not only have their own new attributes or methods, but also the attributes and methods of their parent class.

It is also possible to override properties and methods in a parent class by inheriting subclasses of a class.

/ / 3
class Student extends Person{
    study() {
        return 'study'
    },
    getName() {
        return 'panda'}}const student = new Student();
student.getName();    // panda
Copy the code

In example 3, subclass Student overrides the parent getName() method, so instance Student calls getName() to get panda instead of bear.

The super class

If a subclass overrides a property or method in its parent class, what if the subclass needs to use a property or method in its parent class? This is where super comes in. Super means that if a subclass overrides a method of its parent class, the subclass can still call the method of its parent class.

/ / 4
class Student extends Person{
    study() {
        return 'study'
    }
    getName() {
        return 'My name is ' + super.getName()
    }
}
const student = new Student();
student.getName();    // My name is bear
Copy the code

In example 4, instance student calls getName() from subclass student, which in turn calls getName() from its parent class Person.

Class access type

TypeScript classes have three types of access: public, private, and protected. Public is allowed to be called inside and outside a class. An internal call to a class is an in-class call. A call on an instance is an out-of-class call. Private allows calls inside a class, not outside. Protected is allowed inside a class and in inherited subclasses. In a class, the default access type for properties and methods is public.

/ / case 5
class Person {
    public name = 'bear';
    public getName() {
        return this.name   // Call within class}}const person = new Person();
person.name;               // This is an out-of-class call
Copy the code

Finish this! If this article helps you at all, please give it a thumbs up.