انتقل إلى المحتوى الرئيسي

Classes

TypeScript takes an object-oriented approach and has full class support. The class represents a template for creating objects and encapsulates the functionality that an object must have. A class defines the state and behavior that an object has.

Classes

Let's look at a simple example of working with classes:

class Greeter {
greeting: string // Свойство greeting, тип строка
constructor(message: string) {
// Конструктор класса Greeter
this.greeting = message
}
greet() {
// Метод класса Greeter
return 'Hello, ' + this.greeting
}
}

let greeter = new Greeter('world')

greeter.greet() // Вернет строчку Hello world

A new class Greeter has been declared. This class has three items: a greeting property, a constructor, and a greet method. The last line instantiates the Greeter class using new. It calls the constructor defined earlier, creates a new object, and runs the constructor to initialize it.

Inheritance

TypeScript uses familiar object-oriented programming approaches. One of the most fundamental approaches in class-based programming is the creation of new classes using inheritance.

Example:

class Animal {
name: string
constructor(theName: string) {
this.name = theName
}
move(distanceInMeters: number = 0) {
return this.name + ' moved ' + distanceInMeters + ' m.'
}
}

class Snake extends Animal {
constructor(name: string) {
super(name)
}
move(distanceInMeters = 5) {
super.move(distanceInMeters)
}
}

class Horse extends Animal {
constructor(name: string) {
super(name)
}
move(distanceInMeters = 45) {
super.move(distanceInMeters)
}
}

let sam = new Snake('Sammy the Python')
let tom = new Horse('Tommy the Palomino')

sam.move()
tom.move(34)

Here, the extends keyword is used to create a subclass. The Horse and Snake classes are based on the Animal class and they access its capabilities. The Snake and Horse classes create a move method that overrides the move method from the Animal class, giving it functionality specific to each class.

These are just the basics of working with classes. But in our course we are only getting to know the classes, since the JavaScript library React Native teaches us functional programming.

  1. Metanit
  2. TypeScript-lang leadership TypeScript

Contributors ✨

Thanks goes to these wonderful people (emoji key):


Philipp Dvinyaninov


Dmitriy Vasilev

💵

EnglishMoji!