KeyWord.uts 3.9 KB
Newer Older
杜庆泉's avatar
杜庆泉 已提交
1 2
import { describe, test, expect, Result } from './tests.uts'

3 4 5
class User {
  name : string = "";
  age : number = 0
杜庆泉's avatar
杜庆泉 已提交
6 7
}

lizhongyi_'s avatar
lizhongyi_ 已提交
8 9
const passcode = "secret passcode";
class Parent {
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
  private _name : string = ""; // private是私有的,外部不能访问
  get name() : string { // 读取name会触发此拦截器
    console.log("start to get parent.name");
    return this._name;
  }
  set name(newName : string) { // 给name赋值会触发此拦截器
    console.log("start to set parent.name");
    if (passcode == "secret passcode") { // 校验是否有权修改name的值,这里的条件可以修改以方便测试
      this._name = newName;
    } else {
      console.log("Error: set parent.name fail");
    }
  }

  // 静态属性和方法
  static age : number = 30
  static run() : string {
    console.log("this is a static method")
    return "static method"
  }

  // readonly
  readonly address : string = ""

  weight : number = 80

  // constructor
  constructor(weight : number) {
    console.log("开始实例化")
    this.weight = weight
  }

  // 实例方法
  eat() : string {
    console.log("this is parent")
    return "parent instance method"
  }

lizhongyi_'s avatar
lizhongyi_ 已提交
48 49 50
}

class ChildrenTest extends Parent {
51 52 53 54 55 56 57 58 59 60 61 62 63 64

  // override constructor(weight: number) {
  // 	super(weight)
  // }

  override eat() : string {
    super.eat()
    return "children instance method"
  }

  // swift 中不能重写父类的存储属性,只能重写父类的计算的属性(本质上是重写计算属性的get set方法)
  // #ifndef APP-IOS
  override weight = 8
  // #endif 
lizhongyi_'s avatar
lizhongyi_ 已提交
65 66
}

67 68 69 70 71 72 73 74 75 76 77 78 79
export function testKeyWord() : Result {
  return describe("KeyWord", () => {

    test('new', () => {
      let new1 = new User()
      // expect(JSON.stringify(new1)).toEqual('{"age":0,"name":""}')
      console.log(JSON.stringify(new1))

      new1.age = 10
      new1.name = "job"
      console.log(JSON.stringify(new1))
      // expect(JSON.stringify(new1)).toEqual('{"age":10,"name":"job"}')

杜庆泉's avatar
杜庆泉 已提交
80
    })
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141

    test('typeof', () => {
      let new1 = new User()
      expect(typeof (new1)).toEqual('object')

      // #ifdef APP-ANDROID || APP-IOS
      expect(typeof (123456.789)).toEqual('Double')
      //expect(typeof(789778979798797987979)).toEqual('number')
      expect(typeof (0.0)).toEqual('Double')
      // #endif

      expect(typeof ("hello world")).toEqual('string')
      expect(typeof ([1, 2, 3])).toEqual('object')
      expect(typeof (new Array<any>())).toEqual('object')
      expect(typeof (new Set<any>())).toEqual('object')
      // expect(typeof(new Map<any,any>())).toEqual('object')
      expect(typeof (new Date())).toEqual('object')
      expect(typeof ("hello world")).toEqual('string')
      // 原生对象  
      // #ifdef APP-ANDROID
      expect(typeof (UTSAndroid.getUniActivity())).toEqual('object')
      // #endif


    })


    test('instanceof', () => {
      let user1 : any = new User()
      let instanceRet1 = user1 instanceof User
      expect(instanceRet1).toEqual(true)
      let instanceRet2 = (typeof user1) == "string"
      expect(instanceRet2).toEqual(false)
      let num1 : any = 3.1415926
      let instanceRet3 = (typeof num1) == "string"
      expect(instanceRet3).toEqual(false)
      // #ifdef APP-ANDROID
      let instanceRet4 = (typeof num1) == "number"
      expect(instanceRet4).toEqual(false)
      // #endif
    })

    test('isArray', () => {
      let user1 : any = new User()
      expect(Array.isArray(user1)).toEqual(false)
      expect(Array.isArray([1, 2, 3])).toEqual(true)
    })

    test('class', () => {
      let p = new Parent(20)
      p.name = "tom" // 会打印"start to set person.name"
      console.log(p.name); // 先打印"start to get person.name",然后打印"tom"
      expect(Parent.age).toEqual(30)
      expect(Parent.run()).toEqual("static method")
      expect(p.eat()).toEqual("parent instance method")
      // let c = new ChildrenTest(0)
      // expect(c.eat()).toEqual("children instance method")
    })

  })
}