class 类

class 类

两个特征;

1.静态特征-----属性

2.动态特征-----方法/函数

class类的关键词

person 类名

class Person {
    //构造函数
    constructor(name: string, sex: string, age: number) {
        this.name = name;
        this.sex = sex;
        this.age = age;
    }
    name: string
    sex: string
    age: number
    //方法
    chi() {
        console.log('吃饭')
    }
    he() {
        console.log('喝水')

    }
}

对象 类的实例

创建对象 实例化对象

let lzh: Person = new Person('里奥', '男', 18)

对象数组

1.学生类 学号,姓名,班级 学习

class Student {
    id: string
    name: string
    clas: string
    constructor(id: string, name: string, clas: string) {
        this.id = id
        this.name = name
        this.clas = clas
    }
    study() {
        console.log(this.name + '在学习')
    }
}
//2.把学生存到数组中
let stus: Array<Student> = [];
let stu1: Student = new Student('001', '张三', 'jave');
let stu2: Student = new Student('002', '张4', 'jav');
let stu3: Student = new Student('003', '张5', 'jae');
let stu4: Student = new Student('004', '张6', 'jve');
let stu5: Student = new Student('005', '张7', 'ave');
let stu6: Student = new Student('006', '张8', 'javse');
let stu7: Student = new Student('007', '张9', 'javde');
stus.push(stu1)
stus.push(stu2)
stus.push(stu3)
stus.push(stu4)
stus.push(stu5)
stus.push(stu6)
stus.push(stu7)
//3.遍历数组
for (let i in stus) {
    let s: Student = stus[i];
    console.log(`学号:${s.id},姓名:${s.name},班级:${s.clas}`)
}
class pet {
    name: string
    id: string
    sex: string
    constructor(name: string, id: string, sex: string) {
        this.name = name
        this.id = id
        this.sex = sex

    }
    sj(){
        console.log(this.name + '在睡觉')
    }
    chi(){
        console.log(this.name + '吃')
    }
}
let stu: Array<pet> = [];
let st2: pet = new pet('小白', '土猫', '公');
let st3: pet = new pet('小黑', '阿拉斯加', '母');
let st4: pet = new pet('小喇叭', '啦哈哈', '公');
let st5: pet = new pet('小蓝', '野', '母');
stu.push(st2)
stu.push(st3)
stu.push(st4)
stu.push(st5)
for (let i in stu) {
    let a: pet = stu[i];
    console.log(`姓名:${a.name},类型:${a.id},性别:${a.sex}`)
}
 Column(){
    Text(`字符串:${this.str}`).fontSize(20)
      .onClick(()=>{
        this.str='你好'
      })
    Text(`对象:${this.aa.a},${this.aa.b}`).fontSize(20)
      .onClick(()=>{
       this.aa.a='AAA'
        this.aa.b='BBB'
      })
    Text(`数组长度:${this.as.length}`).fontSize(20)
      .onClick(()=>{
       this.as.push(new Aa('1','1'))
      })
    Text(`数组的数据:${this.as[0].a},${this.as[0].b}`)
      .onClick(()=>{      //@state对于数组对象,无法及时刷新
       this.as[0].a='123'
        this.as[0].b='1234'
        let a:Aa=this.as[0];
       this.as.splice(0,1,a);
      })
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值