DEV Community

Cover image for Introduction to the ArkTS language(6)
liu yang
liu yang

Posted on

Introduction to the ArkTS language(6)

Methods

Instance Methods and Static Methods

Methods belong to classes, which can define both instance methods and static methods. Static methods belong to the class itself and can only access static fields. Instance methods, on the other hand, can access both static and instance fields, including private fields of the class.

Instance Methods

The following example illustrates how instance methods work. The calculateArea method calculates the area of a rectangle by multiplying its height and width:

class RectangleSize {
  private height: number = 0;
  private width: number = 0;
  constructor(height: number, width: number) {
    this.height = height;
    this.width = width;
  }
  calculateArea(): number {
    return this.height * this.width;
  }
}
Enter fullscreen mode Exit fullscreen mode

Instance methods must be called through an instance of the class:

let square = new RectangleSize(10, 10);
square.calculateArea(); // Output: 100
Enter fullscreen mode Exit fullscreen mode

Static Methods

Declare a method as static using the static keyword. Static methods belong to the class itself and can only access static fields. They define the public behavior of the class as a whole. Static methods must be called using the class name:

class Cl {
  static staticMethod(): string {
    return 'this is a static method.';
  }
}
console.log(Cl.staticMethod());
Enter fullscreen mode Exit fullscreen mode

Inheritance

A class can inherit from another class (called the base class) and implement multiple interfaces using the following syntax:

class [extends BaseClassName] [implements listOfInterfaces] {
  // ...
}
Enter fullscreen mode Exit fullscreen mode

The inheriting class inherits the fields and methods of the base class but not its constructor. It can define new fields and methods and can also override methods defined in the base class. The base class is also known as the "parent class" or "superclass," while the inheriting class is also called the "derived class" or "subclass."

Example:

class Person {
  name: string = '';
  private _age = 0;
  get age(): number {
    return this._age;
  }
}
class Employee extends Person {
  salary: number = 0;
  calculateTaxes(): number {
    return this.salary * 0.42;
  }
}
Enter fullscreen mode Exit fullscreen mode

A class with an implements clause must implement all methods defined in the listed interfaces, except for those with a default implementation.

interface DateInterface {
  now(): string;
}
class MyDate implements DateInterface {
  now(): string {
    // Implementation here
    return 'now';
  }
}
Enter fullscreen mode Exit fullscreen mode

Accessing the Parent Class

The super keyword is used to access the instance fields, instance methods, and constructor of the parent class. When implementing subclass functionality, this keyword allows you to obtain the necessary interfaces from the parent class:

class RectangleSize {
  protected height: number = 0;
  protected width: number = 0;

  constructor(h: number, w: number) {
    this.height = h;
    this.width = w;
  }

  draw() {
    /* Draw border */
  }
}
class FilledRectangle extends RectangleSize {
  color = '';
  constructor(h: number, w: number, c: string) {
    super(h, w); // Call to the parent class constructor
    this.color = c;
  }

  draw() {
    super.draw(); // Call to the parent class method
    // super.height - accessible here
    /* Fill rectangle */
  }
}
Enter fullscreen mode Exit fullscreen mode

Method Overriding

A subclass can override the implementation of a method defined in its parent class. The overridden method must have the same parameter types and the same or a derived return type as the original method.

class RectangleSize {
  // ...
  area(): number {
    // Implementation
    return 0;
  }
}
class Square extends RectangleSize {
  private side: number = 0;
  area(): number {
    return this.side * this.side;
  }
}
Enter fullscreen mode Exit fullscreen mode

Method Overload Signatures

Specify different ways to call a method using overload signatures. This is done by declaring multiple method heads with the same name but different signatures for the same method, followed by the method implementation.

class C {
  foo(x: number): void;            /* First signature */
  foo(x: string): void;            /* Second signature */
  foo(x: number | string): void {  /* Implementation signature */
  }
}
let c = new C();
c.foo(123);     // OK, uses the first signature
c.foo('aa'); // OK, uses the second signature
Enter fullscreen mode Exit fullscreen mode

It is an error if two overload signatures have the same name and parameter list.

Top comments (0)