赞
踩
一夜之间鸿蒙崛起了。大家都在学鸿蒙。并且就业岗位的工资那是杠杠的呀。去年一年没有写博客了。去年夫人给生了个宝宝。忙着照顾夫人和宝宝了。太忙了。根本没有时间写博客。今年要坚持学习。为了给孩子赚奶粉钱。努力学习新知识。学习鸿蒙。希望鸿蒙赶紧出基于pc的操作系统。这样就可以一统天下了。
鸿蒙的语言采用 ArkTs,ArkTs继承了TypeScript。TypeScript 是一种基于 JavaScript 构建的强类型编程语言。并且在前年我学习cocos的时候。就是用ts写的小游戏。小游戏是写的差不多了。奈何腾讯云开发收费了。我的小游戏暂且搁浅了。但是和ts咱们还是相知相识的。先简单的复习一下。
ts语法基础回顾
- // 字符串
- let my1: string = "中国" //块级作用域
- var my2: string = "我" //全局作用域
- const my3: string = "世界" //常量不能赋值了
-
- //布尔类型
- let bool1: boolean = true
-
- //数字类型
- let num: number = 900
-
- // 数组
- let list: Array<string> = ["ni", "wo", "ta"]
- let datalist: string[] = ["中国", "地球", "世界"]
-
- //联合类型
- let uni1: string | number | boolean = 100
- let
- uni2:Array < string | number | boolean >= [1, 3, true, "nihao"]
- let uni3: (string | number)[] = ["woaizhonguo", 9999]
-
- // 任意类型
- let every: any = "forever"
-
- //枚举
- enum zimu {
- "C", "D", "E"
- }
-
- enum zimu2 {
- "c" = 1,
- "d" = 2,
- "e" = 3
- }
-
- //对象 ?:表示可选的
- let school: {
- name: string,
- location?: string,
- age: number,
- isNiuBi: boolean
- } = {
- name: "清华大学", location: "北京", age: 100, isNiuBi: true
- }
-
- //接口
- interface car{
- name:string ,
- age:number,
- type:string,
- location?:string ,//可选
- [other:string ]:any//任意类型,可以扩展属性
- }
-
- let jili:car={
- name:"帝豪",
- age:10,
- type:"电动"
- }
- jili.location="天津"
- jili.speed="1000km/h"
-
- //判断语句
- let name="harmonyos"
- if (name==="鸿蒙"){//=== 既判断类型又判断值
- console.log("hi")
- }else if(name=="harmonyos"){
- console.log("yes")
- }
-
- // for 循环
- let datalist1: string[] = ["中国", "地球", "世界"]
- for (let i of datalist1) {//for 拿到的是内容
- console.log(i)
- }
-
- for(let i in datalist1){//in 拿到的是角标索引
- console.log(datalist1[i])
- }
-
- //普通函数
- function say(){
- console.log("hello harmonyos")
- }
- say()
-
- //箭头函数
- const text = () => {
- console.log("hello 箭头函数")
- }
- text()
-
- //带限定的箭头函数
- const test = (type: number): string => {
- var text = "你好 鸿蒙"
- switch (type) {
- case 1:
- text = "鸿蒙爸爸"
- break;
- case 2:
- text = "鸿蒙爷爷"
- break
- default:
- text = "鸿蒙先人"
- break
- }
- return text;
- }
- console.log(test(3))

- //class类
- class Area {
- public id: string;
- protected x: number;
- private y: number;
- public width: number;
- public height: number;
-
- constructor(id: string, x: number, y: number, width: number, height: number) {
- this.id = id
- this.x = x
- this.y = y
- this.width = width
- this.height = height
- }
-
- drawLocation(): number {
- return (this.x) * (this.y)
- }
- }
-
- let a = new Area("tv", 0, 0, 1080, 1920)
- console.log(a.x)
-
- class littleArea extends Area {
- public angle: number
- constructor(id: string, x: number, y: number, width: number, height: number, angle: number) {
- this.id = id
- this.x = x
- this.y = y
- this.width = width
- this.height = height
- this.angle=angle
- }
- drawLocation(): number {
- return (this.x) * (this.y)*(this.angle)
- }
- }
- let little=new littleArea("phone",0,0,540,960,60)
- console.log(little.x)
-
-
-
- //接口
- interface Study {
- tiantian()
- }
- class yuwen implements Study {
- tiantian() {
- console.log("我爱学语文")
- }
- }
-
- class shuxue implements Study {
- tiantian() {
- console.log("我爱学数学")
- }
- }
-
- function init(xs: Study) {
- xs.tiantian()
- }
-
- init(new yuwen())
- init(new shuxue())

-
- //泛型类
- class Cache<T> {
- private map = {}
-
- setCache(key: string, value: T) {
- this.map[key] = value
- }
-
- getCache(key: string) {
- return this.map[key]
- }
- }
- let obj=new Cache<string>()
-
- obj.setCache("nihao ","我爱祖国")
- obj.getCache("nihao ")

导出utils对象和方法
-
- const school={
- student:"留白的云",
- teacher:"孔子",
- time:111
- }
-
- function read(){
- console.log("知之为知之,不知为不知是知也")
- }
-
- // 导出这个对象
- export default school
- // 导出这个方法
- export {read}
导出到华为的ets中,和js\微信小程序的导出一样一样的。
-
- import '../ts/hello.ts'
- import School from '../ts/utils'
- console.log(School.student)
- import { read as Read} from '../ts/utils'
- Read()
-
- @Entry
- @Component
- struct Index {
- @State message: string = 'Hello Harmonyos'
-
- build() {
- Row() {
- Column() {
- Text(this.message)
- .fontSize(40)
- .fontWeight(FontWeight.Bold)
- }
- .width('100%')
- .height('50%')
- }
- .height('100%')
- .width('90%')
- }
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。