【问题描述】
ETS如何创建实体类和引用实体类
【问题答案】
创建一个Student.ts文件,包含name和id属性,代码如下
- export default class student{
- name:string
- id:number
- constructor(name:string,id:number){
- this.name=name
- this.id=id
- }
- }
在page文件夹创建一个TestPage的界面,初始化该实体类和并输出该实体类的成员变量
1.1导入该实体类
import Student from "./student"
1.2实例化该实体类,代码如下
let mystudent=new Student("luck",1)
全部代码如下
- import Student from "./student"
- @Entry
- @Component
- struct Testpage {
- @State message: string = 'Hello World'
-
- public aboutToAppear(){
- let mystudent=new Student("luck",1)
- console.log(JSON.stringify(mystudent))
- }
-
- build() {
- Row() {
- Column() {
- Text(this.message)
- .fontSize(50)
- .fontWeight(FontWeight.Bold)
- }
- .width('100%')
- }
- .height('100%')
- }
- }
运行效果如下

欲了解更多更全技术文章,欢迎访问https://developer.huawei.com/consumer/cn/forum/?ha_source=zzh