DEV Community

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

Posted on

Introduction to the ArkTS language(7)

Constructors, Visibility Modifiers, and Object Literals

Constructors

A class declaration may include a constructor for initializing the object's state. The constructor is defined as follows:

constructor([parameters]) {
  // ...
}
Enter fullscreen mode Exit fullscreen mode

If no constructor is defined, a default constructor with an empty parameter list is automatically created. For example:

class Point {
  x: number = 0;
  y: number = 0;
}
let p = new Point();
Enter fullscreen mode Exit fullscreen mode

In this case, the default constructor initializes the fields in the instance using the default values of the field types.

Constructors in Derived Classes

The first statement in a constructor body can use the super keyword to explicitly call the constructor of the direct parent class.

class RectangleSize {
  constructor(width: number, height: number) {
    // ...
  }
}
class Square extends RectangleSize {
  constructor(side: number) {
    super(side, side);
  }
}
Enter fullscreen mode Exit fullscreen mode

Constructor Overload Signatures

We can specify different ways to call a constructor by writing overload signatures. This is done by declaring multiple constructor heads with the same name but different signatures for the same constructor, followed by the constructor implementation.

class C {
  constructor(x: number)             /* First signature */
  constructor(x: string)             /* Second signature */
  constructor(x: number | string) {  /* Implementation signature */
  }
}
let c1 = new C(123);      // OK, uses the first signature
let c2 = new C('abc');    // 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.

Visibility Modifiers

Both methods and properties of a class can use visibility modifiers. The visibility modifiers include private, protected, and public. The default visibility is public.

Public

Members (fields, methods, constructors) modified by public are visible wherever the class is accessible in the program.

Private

Members modified by private cannot be accessed outside the class in which they are declared. For example:

class C {
  public x: string = '';
  private y: string = '';
  set_y(new_y: string) {
    this.y = new_y; // OK, as y is accessible within the class itself
  }
}
let c = new C();
c.x = 'a'; // OK, as the field is public
c.y = 'b'; // Compile-time error: 'y' is not visible
Enter fullscreen mode Exit fullscreen mode

Protected

The protected modifier is similar to private, but members modified by protected can be accessed within derived classes. For example:

class Base {
  protected x: string = '';
  private y: string = '';
}
class Derived extends Base {
  foo() {
    this.x = 'a'; // OK, accessing the protected member
    this.y = 'b'; // Compile-time error, 'y' is not visible as it is private
  }
}
Enter fullscreen mode Exit fullscreen mode

Object Literals

An object literal is an expression used to create class instances and provide some initial values. It is more convenient in certain situations and can be used as an alternative to new expressions. An object literal is represented by a list of 'property name: value' pairs enclosed in curly braces {}.

class C {
  n: number = 0;
  s: string = '';
}

let c: C = { n: 42, s: 'foo' };
Enter fullscreen mode Exit fullscreen mode

ArkTS is a statically - typed language. As shown in the above example, object literals can only be used in contexts where the type of the literal can be inferred. Other valid examples include:

class C {
  n: number = 0;
  s: string = '';
}

function foo(c: C) {}

let c: C;

c = { n: 42, s: 'foo' };  // Using the type of the variable
foo({ n: 42, s: 'foo' }); // Using the type of the parameter

function bar(): C {
  return { n: 42, s: 'foo' }; // Using the return type
}
Enter fullscreen mode Exit fullscreen mode

They can also be used in array element types or class field types:

class C {
  n: number = 0;
  s: string = '';
}
let cc: C[] = [{ n: 1, s: 'a' }, { n: 2, s: 'b' }];
Enter fullscreen mode Exit fullscreen mode

Object Literals of Record Type

The generic Record is used to map properties of one type (the key type) to another type (the value type). Object literals are often used to initialize values of this type:

let map: Record<string, number> = {
  'John': 25,
  'Mary': 21,
}

map['John']; // 25
Enter fullscreen mode Exit fullscreen mode

Type K can be a string or number type, while V can be any type.

interface PersonInfo {
  age: number;
  salary: number;
}
let map: Record<string, PersonInfo> = {
  'John': { age: 25, salary: 10 },
  'Mary': { age: 21, salary: 20 }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)