当前位置:   article > 正文

学习鸿蒙基础(1)

学习鸿蒙基础(1)

一夜之间鸿蒙崛起了。大家都在学鸿蒙。并且就业岗位的工资那是杠杠的呀。去年一年没有写博客了。去年夫人给生了个宝宝。忙着照顾夫人和宝宝了。太忙了。根本没有时间写博客。今年要坚持学习。为了给孩子赚奶粉钱。努力学习新知识。学习鸿蒙。希望鸿蒙赶紧出基于pc的操作系统。这样就可以一统天下了。

鸿蒙的语言采用 ArkTs,ArkTs继承了TypeScript。TypeScript 是一种基于 JavaScript 构建的强类型编程语言。并且在前年我学习cocos的时候。就是用ts写的小游戏。小游戏是写的差不多了。奈何腾讯云开发收费了。我的小游戏暂且搁浅了。但是和ts咱们还是相知相识的。先简单的复习一下。

ts语法基础回顾

  1. // 字符串
  2. let my1: string = "中国" //块级作用域
  3. var my2: string = "我" //全局作用域
  4. const my3: string = "世界" //常量不能赋值了
  5. //布尔类型
  6. let bool1: boolean = true
  7. //数字类型
  8. let num: number = 900
  9. // 数组
  10. let list: Array<string> = ["ni", "wo", "ta"]
  11. let datalist: string[] = ["中国", "地球", "世界"]
  12. //联合类型
  13. let uni1: string | number | boolean = 100
  14. let
  15. uni2:Array < string | number | boolean >= [1, 3, true, "nihao"]
  16. let uni3: (string | number)[] = ["woaizhonguo", 9999]
  17. // 任意类型
  18. let every: any = "forever"
  19. //枚举
  20. enum zimu {
  21. "C", "D", "E"
  22. }
  23. enum zimu2 {
  24. "c" = 1,
  25. "d" = 2,
  26. "e" = 3
  27. }
  28. //对象 ?:表示可选的
  29. let school: {
  30. name: string,
  31. location?: string,
  32. age: number,
  33. isNiuBi: boolean
  34. } = {
  35. name: "清华大学", location: "北京", age: 100, isNiuBi: true
  36. }
  37. //接口
  38. interface car{
  39. name:string ,
  40. age:number,
  41. type:string,
  42. location?:string ,//可选
  43. [other:string ]:any//任意类型,可以扩展属性
  44. }
  45. let jili:car={
  46. name:"帝豪",
  47. age:10,
  48. type:"电动"
  49. }
  50. jili.location="天津"
  51. jili.speed="1000km/h"
  52. //判断语句
  53. let name="harmonyos"
  54. if (name==="鸿蒙"){//=== 既判断类型又判断值
  55. console.log("hi")
  56. }else if(name=="harmonyos"){
  57. console.log("yes")
  58. }
  59. // for 循环
  60. let datalist1: string[] = ["中国", "地球", "世界"]
  61. for (let i of datalist1) {//for 拿到的是内容
  62. console.log(i)
  63. }
  64. for(let i in datalist1){//in 拿到的是角标索引
  65. console.log(datalist1[i])
  66. }
  67. //普通函数
  68. function say(){
  69. console.log("hello harmonyos")
  70. }
  71. say()
  72. //箭头函数
  73. const text = () => {
  74. console.log("hello 箭头函数")
  75. }
  76. text()
  77. //带限定的箭头函数
  78. const test = (type: number): string => {
  79. var text = "你好 鸿蒙"
  80. switch (type) {
  81. case 1:
  82. text = "鸿蒙爸爸"
  83. break;
  84. case 2:
  85. text = "鸿蒙爷爷"
  86. break
  87. default:
  88. text = "鸿蒙先人"
  89. break
  90. }
  91. return text;
  92. }
  93. console.log(test(3))

  1. //class类
  2. class Area {
  3. public id: string;
  4. protected x: number;
  5. private y: number;
  6. public width: number;
  7. public height: number;
  8. constructor(id: string, x: number, y: number, width: number, height: number) {
  9. this.id = id
  10. this.x = x
  11. this.y = y
  12. this.width = width
  13. this.height = height
  14. }
  15. drawLocation(): number {
  16. return (this.x) * (this.y)
  17. }
  18. }
  19. let a = new Area("tv", 0, 0, 1080, 1920)
  20. console.log(a.x)
  21. class littleArea extends Area {
  22. public angle: number
  23. constructor(id: string, x: number, y: number, width: number, height: number, angle: number) {
  24. this.id = id
  25. this.x = x
  26. this.y = y
  27. this.width = width
  28. this.height = height
  29. this.angle=angle
  30. }
  31. drawLocation(): number {
  32. return (this.x) * (this.y)*(this.angle)
  33. }
  34. }
  35. let little=new littleArea("phone",0,0,540,960,60)
  36. console.log(little.x)
  37. //接口
  38. interface Study {
  39. tiantian()
  40. }
  41. class yuwen implements Study {
  42. tiantian() {
  43. console.log("我爱学语文")
  44. }
  45. }
  46. class shuxue implements Study {
  47. tiantian() {
  48. console.log("我爱学数学")
  49. }
  50. }
  51. function init(xs: Study) {
  52. xs.tiantian()
  53. }
  54. init(new yuwen())
  55. init(new shuxue())
  1. //泛型类
  2. class Cache<T> {
  3. private map = {}
  4. setCache(key: string, value: T) {
  5. this.map[key] = value
  6. }
  7. getCache(key: string) {
  8. return this.map[key]
  9. }
  10. }
  11. let obj=new Cache<string>()
  12. obj.setCache("nihao ","我爱祖国")
  13. obj.getCache("nihao ")

导出utils对象和方法

  1. const school={
  2. student:"留白的云",
  3. teacher:"孔子",
  4. time:111
  5. }
  6. function read(){
  7. console.log("知之为知之,不知为不知是知也")
  8. }
  9. // 导出这个对象
  10. export default school
  11. // 导出这个方法
  12. export {read}

 导出到华为的ets中,和js\微信小程序的导出一样一样的。

  1. import '../ts/hello.ts'
  2. import School from '../ts/utils'
  3. console.log(School.student)
  4. import { read as Read} from '../ts/utils'
  5. Read()
  6. @Entry
  7. @Component
  8. struct Index {
  9. @State message: string = 'Hello Harmonyos'
  10. build() {
  11. Row() {
  12. Column() {
  13. Text(this.message)
  14. .fontSize(40)
  15. .fontWeight(FontWeight.Bold)
  16. }
  17. .width('100%')
  18. .height('50%')
  19. }
  20. .height('100%')
  21. .width('90%')
  22. }
  23. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/45930?site
推荐阅读
相关标签
  

闽ICP备14008679号