DEV Community

Cover image for HarmonyOS development: code generation in DevEcoStudio
程序员一鸣
程序员一鸣

Posted on

HarmonyOS development: code generation in DevEcoStudio

Foreword 

this article is based on DevEco Studio 5.0.5 Release 

devEcoStudio, like most development tools, has tools or methods to improve code coding efficiency. I don't know if you have put it into use in actual development. In this article, we focus on combining actual code and look at the shortcuts in DevEcoStudio that can improve our coding efficiency. 

Quickly generate declaration information to Index file 

when we are developing dynamic shared packages or static shared packages, we need to declare variables, methods, interfaces, classes and other information that need to be exposed to the outside world. Index file, only can be convenient for other modules or others to call , The most common way is to manually copy the statements one by one. Manual copy is no problem, but the efficiency is very low, especially for those packages that need to expose many methods, it is time-consuming and laborious, and very inconvenient. 

In fact, DevEcoStudio provides us with a quick declaration method. We only need to right-click in a file to select Generate, select Declarations, or use the shortcut key Alt + Insert, select Declarations in the menu, and then select the variable name, method name, interface name, class name, etc. that need to be declared to Generate corresponding declaration information in batch in the Index.ets file of the module. 

Right-click and select Generate: 

Image description

select Declarations 

Image description

select the information to declare, you can make multiple selections.

Image description

Quickly Generate Constructors 

I believe there must be a small partner who is still writing the constructor when writing the entity class. In fact, in DevEco Studio, this generation can be done in a shortcut. 

In the class where you need to Generate a Constructor, use the shortcut key Alt + Insert, or click the right mouse button to select Generate, then select Constructor, then select one or more parameters that need to Generate a Constructor, and click OK to quickly implement a Constructor. 

Right-click and select Generate: 

Image description

select Constructor 

Image description

select attributes to carry 

Image description

the dynamic effects are as follows: 

Image description

quickly generate get/set methods 

in DevEco Studio, in addition to Constructor generation, we can also automatically generate get and set methods corresponding to member variables or object properties in the current class, which is convenient for external calls. 

The generation method is basically the same as the steps of the constructor, except that Getter or Setter is selected here, or Getter and Setter, and you can choose according to your own needs. 

Image description

Select the attributes you want to generate. 

Image description

The corresponding get and set methods are automatically generated:

 

class Test {
  private _name?: string | undefined

  public set name(value: string | undefined) {
    this._name = value
  }

  public get name(): string | undefined {
    return this._name
  }

  private _color?: ResourceColor | undefined

  public set color(value: ResourceColor | undefined) {
    this._color = value
  }

  public get color(): ResourceColor | undefined {
    return this._color
  }

}
Enter fullscreen mode Exit fullscreen mode

quick Overriding Parent

In development, if one of your classes inherits a parent class and needs to overwrite the Methods or properties in the parent class, you can use DevEco Studio to provide the Override Methods shortcut, which is basically the same as the above steps. 

Place the cursor at the subclass definition position, use the shortcut key Ctrl + O, or right-click Generate, then select the Override Methods option, select the Methods, variables, etc. in the specified object to be overwritten, and click OK to automatically overwrite. 

Select Override Methods option 

Image description

select to specify the information that needs to be overwritten. 

Image description

Related Summary 

in fact, you can find that an article is based on the Generate option after right clicking, so it is still very simple. Of course, I still hope that the above functions can be applied in actual development instead of staying on paper. 

This article label: Hongmeng Development Tools/DevEco Studio

Top comments (0)