当前位置: 首页>后端>正文

HarmonyOs开发体验

Harmony 鍙戝睍

楦胯挋鐨勫彂灞曞叾瀹炴湁涓€娈垫椂闂翠簡锛屽苟涓旈殢鐫€楦胯挋鐨勫彂灞曚竴鐩撮兘鏈変袱鏂归潰鐨勪簤璁紝涓€鏂硅涓洪缚钂欐槸鍩轰簬Android寮€鍙戠殑鎿嶄綔绯荤粺锛岃川鐤戦缚钂欏彧鏄妸澶栬〃鏀逛簡鐨凙ndroid锛涘彟涓€鏂瑰垯璁や负楦胯挋鏄崕涓鸿嚜涓荤爺鍙戠殑鎿嶄綔绯荤粺锛屽湪鏅鸿兘鎵嬫満鍜屾櫤鑳藉钩鏉垮簲鐢ㄤ笂閲囩敤鐨勪笌Android鍏煎銆?/p>

闅忕潃浠婂勾8鏈堜唤 HarmoneyOs4鐨勫彂甯冿紝骞惰〃绀哄悗缁殑Harmony NEXT灏嗕笉鍩轰簬Android锛岃繖涓€绯诲垪鐨勪簤璁虹湅璧锋潵宸茬粡缁撴潫浜嗐€傚崕涓哄畼鏂瑰绉颁笉鍏煎Android鍚庡皢鍘嬬缉浼樺寲40%鐨勪唬鐮侊紝搴旇鎸囩殑灏辨槸HarmonyOs涓吋瀹笰ndroid鐨勯偅閮ㄥ垎AOSP鐨勪唬鐮併€?/p>

鍥犱负濡傛灉瑕佸吋瀹笰ndroid鐨勮瘽锛岃繖閮ㄥ垎AOSP鐨勪唬鐮佹槸鑲畾浼氳瀛樺湪鐨勶紝绔欏湪鍗庝负鐨勭珛鍦轰篃鍙互鐞嗚В锛屾瘮杈冪郴缁熺殑鎺ㄥ箍绂讳笉寮€鐢熸€侊紝鍞竴鐨勫姙娉曪紝灏辨槸鍦ㄥ畨鍗撶殑鍩虹涓婏紝鍏堣繘琛岄€愭鐨勬浛鎹慨鏀癸紝淇濇寔瀵瑰畨鍗撶殑鍏煎鎬э紝闄嶄綆APP鎻愪緵鍟嗙殑寮€鍙戞垚鏈紝璁╃郴缁熷厛闈笘銆傜劧鍚庣郴缁熼潰涓栧悗锛岄€愭鎵╁ぇ鐢ㄦ埛鏁伴噺锛屾彁鍗囩煡鍚嶅害锛屾墿澶у奖鍝嶅姏銆傛湁浜嗕竴瀹氱殑褰卞搷鍔涘拰鍩虹锛屾墠鑳藉幓鍜屾洿澶氱殑APP鎻愪緵鍟嗗拰寮€鍙戣€呭悎浣滐紝璁╀粬浠拡瀵规垜鐨勭郴缁燂紝鍘诲紑鍙慉PP锛屾瀯寤虹敓鎬併€?/p>

鐓х洰鍓嶆潵鐪嬶紝楦胯挋宸茬粡鏄蛋鍒颁簡鏋勫缓鐢熸€佺殑杩欎竴姝ユ潵浜嗐€?/p>

鎴戜滑鍙互鐪嬩笅楦胯挋寮€鍙戣€呯殑瀹樼綉,鍏跺疄宸茬粡寰堝畬鍠勪簡銆備粠IDE锛屽埌鐩稿叧鐨勮绋嬫枃妗o紝鍒癈odelabs閮芥槸閰嶅榻愬叏鐨勶紝瀹屽叏澶熸垜浠紑鍙戣€呬笂鎵嬪拰鍏ラ棬浜嗭紝骞朵笖鐢变簬鏄腑鏂囨枃妗o紝纭疄浣撻獙姣旇胺姝岀殑鏈哄櫒缈昏瘧鐨勬枃妗h鏄撴噦涓嶅皯馃槀銆?/p>

Harmony 寮€鍙?/h2>
  • 涓ユ牸鏉ヨ锛孒armony 寮€鍙戝簲璇ユ寚鐨勬槸涓ら儴鍒嗭紝搴旂敤寮€鍙?鍜?璁惧寮€鍙戙€傝澶囧紑鍙戞寚鐨勬槸姣斿鑺墖/妯$粍/寮€鍙戞澘涔嬬被鐨勭‖浠朵笌楦胯挋鐨勫吋瀹规垨閫氳锛屽満鏅槸鏅鸿兘瀹跺眳涓€绫荤殑锛屾瘯绔烵penHarmony鍒濊》鏄竾鐗╀簰鑱斿槢銆?鑰屾垜浠繖閲岃鐨勬槸搴旂敤寮€鍙戙€?/li>

IDE 鍜?寮€鍙戣瑷€

棣栧厛寮€鍙戠敤鐨処DE鏄疕UAWEI DevEco Studio锛屾槸鍩轰簬IDEA寮€鍙戠殑锛屾墍浠ュ拰Android Studio姣旇捣鏉ワ紝涓嶈兘璇存鏃犲叧绯伙紝鍙兘璇存槸涓€妯′竴鏍枫€傝繖瀵瑰畨鍗撳紑鍙戣€呮潵璇磋繕鏄緢鍙嬪ソ鐨勩€?/p>

鐒跺悗寮€鍙戣瑷€鏄?code>arkts,瀹冩槸鍩轰簬TypeScript锛堢畝绉癟S锛夊熀纭€涓婏紝瀵筎S鐨勫姩鎬佺被鍨嬬壒鎬ф柦鍔犳洿涓ユ牸鐨勭害鏉燂紝寮曞叆闈欐€佺被鍨嬨€傚苟涓斿€熷姪ArkUI鐨勬鏋讹紝鎻愪緵浜嗗0鏄庡紡UI銆佺姸鎬佺鐞嗙殑鑳藉姏锛岀敤璧锋潵杩樻槸姣旇緝绠€娲佺殑銆?/p>

寮€鍙戜綋楠?/h2>

楦胯挋鍘熺敓鐨勫紑鍙戯紝瀹樻柟缁欏嚭浜嗕袱濂楀紑鍙戞ā鍨嬶紝涓€濂楁槸鍩轰簬Java鎴朖s鐨凢A妯″瀷锛屽搴旂殑鐗堟湰鏄疉PI 8涔熷氨鏄疕armony3.0鍙婁互鍓嶏紝杩欑灏辨槸xxAbility,琚悙妲界湅璧锋潵寰堝儚Android鐨凙ctivity鐨勫紑鍙戞柟寮忋€?/p>

涓嶈繃闅忕潃楦胯挋鐨勫彂灞曪紝Java宸茬粡涓嶅啀鍙楁敮鎸佷簡锛岀幇鍦ㄥ畼鏂规帹鑽愮殑灏辨槸鍒氭墠鎻愬埌鐨勫熀浜嶢rkTs鐨凷tage妯″瀷锛屾彁鍑轰簡AbilityStage銆乄indowStage绛夌被浣滀负搴旂敤缁勪欢鍜學indow绐楀彛鐨勨€滆垶鍙扳€濓紝涓や釜妯″瀷鐨勫樊寮傚畼缃戜篃缁欏嚭浜嗚缁嗙殑璇存槑锛屾垜杩欓噷渚濈劧浣跨敤鐨勫畼鏂逛富鎺ㄧ殑Stage妯″瀷寮€鍙戙€?/p>

ArkUi寮€鍙戜綋楠?/h3>

涓€涓畝鍗曠殑璁℃暟鍣―emo,鍊熷姪arkTs鐨勬敞瑙o紙ts閲岄潰鍙楗板櫒锛夊畬鎴愬鐘舵€佺殑绠$悊锛寀i寮€鍙戠敤鐨勬槸澹版槑寮忕殑璇硶锛岀啛鎮塅lutter鎴栬€匯eact鐨勫悓瀛﹀簲璇ュ緢浜插垏銆傛暣涓啓娉曠湅璧锋潵杩樻槸寰堢畝娲佺殑銆?/p>

@Entry
@Component
struct ParentComponent {
  @State number: number = 0

  build() {
    Stack() {
      Text(`${this.number}`)
        .fontSize(26)
        .position({ x: '50%', y: '50%' })
      Button() {
        Image($r('app.media.ic_public_add'))
          .width(50)
          .height(50)
      }
      .width(60)
      .height(60)
      .position({ x: '80%', y: 600 })
      .type(ButtonType.Circle)
      .backgroundColor(0xFFFFFF)
      .onClick(() => {
        this.number++
      })
    }
  }
}

TypeScript

绠€鍗曚粙缁嶄竴涓婽ypeScript锛孴ypeScript鏄敱寰蒋寮€鍙戠殑涓€娆惧紑婧愮殑缂栫▼璇█銆?鍏跺畼鏂瑰瀹冪殑瀹氫箟鏄疶yped JavaScript at Any Scale.娣诲姞浜嗙被鍨嬬郴缁熺殑 JavaScript锛岄€傜敤浜庝换浣曡妯$殑椤圭洰銆?/p>

棣栧厛Ts瀹屽叏鍏煎Js,Typed鐨勬剰鎬濇槸鍦↗avaScript鐨勫熀纭€涓婏紝澧炲姞浜嗛潤鎬佺被鍨嬬殑妫€鏌ャ€?/p>

鍔ㄦ€?闈欐€佺被鍨?/h4>

鎴戜滑鐭ラ亾璇█鎸夌収銆岀被鍨嬫鏌ョ殑鏃舵満銆嶆潵鍒嗙被锛屽彲浠ュ垎涓哄姩鎬佺被鍨嬪拰闈欐€佺被鍨嬨€傚姩鎬佺被鍨嬫槸鎸囧湪杩愯鏃舵墠浼氳繘琛岀被鍨嬫鏌ワ紝浼樼偣鏄啓浠g爜寰堢伒娲绘柟渚匡紝涓嶇敤鍘诲啓闈炲父澶氱殑绫诲瀷瀹氫箟锛岀己鐐瑰氨鏄唬鐮佽川閲忓弬宸笉榻愶紝缁存姢鎴愭湰楂橈紝姣斿鏈変簺閿欒鍦ㄧ紪璇戞湡涓嶄細鎶ラ敊锛岃€屽湪杩愯鏃朵細鎶ラ敊銆傝繖鏍风殑缂虹偣瀵逛簬涓€涓ぇ鍨嬮」鐩潵璇存槸寰堣嚧鍛界殑銆?/p>

鑰孴ypeScript 鏄潤鎬佺被鍨嬬殑璇█锛屾墍浠ヨ兘鍦ㄧ紪璇戦樁娈靛氨浼氳繘琛岀被鍨嬫鏌ワ紝鏈夐棶棰樺氨浼氭姤閿欙紝涓哄紑鍙戣€呮彁渚涗簡渚垮埄銆?/p>

寮?寮辩被鍨?/h4>

寮虹被鍨嬫槸鎸囧彉閲忓畾涔夊悗锛屼笉鍏佽鏀瑰彉鍙橀噺鐨勬暟鎹被鍨嬶紝闄ら潪杩涜寮哄埗绫诲瀷杞崲銆傜浉鍙嶏紝寮辩被鍨嬪氨鏄寚鍙橀噺鐨勫€煎叾绫诲瀷鏄娉涚殑锛屽彲浠ヨ璧嬪€煎绉嶇被鍨嬬殑鍊笺€?br> 涓句緥鏉ヨ杩欐浠g爜鍦╦s涓槸鍙互姝e父杩愯鐨勶細

let a = 1;
a = 'a';

鎸夌悊鏉ヨ锛宼s鏄畬鍏ㄥ吋瀹筳avaScript鐨勶紝鏃㈢劧瀹屽叏鍏煎锛岄偅涔堢収缃戜笂鐨勪竴浜涜娉曪紝ts搴旇涔熸槸灞炰簬寮辩被鍨嬬殑璇█銆?/p>

浣嗘槸涓婇潰杩欐浠g爜鏀惧湪ts涓紝缂栬瘧鍣ㄥ氨浼氭彁绀?Type 'string' is not assignable to type 'number'.锛堜笉鑳藉皢绫诲瀷鈥渟tring鈥濆垎閰嶇粰绫诲瀷鈥渘umber鈥? 杩欎釜鍏跺疄鏄疎SLint 鎻愪緵鐨勪唬鐮佹鏌ュ姛鑳?鎴戣瘯浜嗕竴涓嬶紝鍥犱负杩欐浠g爜铏界劧椋樼孩锛屼絾鏄繍琛岃捣鏉ユ槸娌¢棶棰樼殑銆?/p>

涔熷氨鏄ts铏界劧鏄急绫诲瀷鐨勮瑷€锛屼絾鍦ㄥ紑鍙戠殑浣撻獙涓婏紝鏄兘閬垮厤鍐欏嚭寮辩被鍨嬬殑浠g爜鐨勩€?/p>

鎬讳箣锛宼s灏辨槸瀵笿s鍔犱簡绫诲瀷鐨勬鏌ワ紝鎵€浠s姣斾箣浜巎s浼氭洿閫傚悎澶у瀷椤圭洰鐨勫紑鍙戜笌缁存姢銆傝€岄缚钂欑殑ArkTs涔熸槸瀹屽叏鍏煎Ts璇硶鐨勩€?/p>

璇█鍙戝睍

涓婇潰璇寸殑涓€浜涘姣斿彲浠ョ湅鍑簀avaScript鐨勪竴浜涗笉瓒冲拰瀵瑰ぇ鍨嬮」鐩紑鍙戠殑闄愬埗锛岃繖閲屼篃椤轰究鑱婅亰TypeScript鐨勫彂灞曟晠浜嬨€?/p>

2011 骞?9 鏈?缃戠粶涓婂嚭鐜颁簡涓€灏佹爣棰樹负"Future of JavaScript"鐨勮胺姝屽唴閮ㄩ偖浠?閭欢涓〃鏄?鐢变簬 Javascript 璇█鍙戝睍缂撴參,璋锋瓕鍐呴儴姝e湪寮€鍙戜竴闂ㄦ瘮 JavaScript 鏇村ソ鐨?web 璇█銆?/p>

杩欓棬鏂拌瑷€鐨勭洰鏍囨槸瀹炵幇 JavaScript 鎵€鑳藉疄鐜扮殑涓€鍒囧姛鑳姐€傚畠鐨勪富瑕佺洰鏍囨槸"淇濇寔 JavaScript 鐨勫姩鎬佺壒鎬?浣嗚鏈夋洿濂界殑鎬ц兘锛岄厤缃枃浠讹紝骞惰兘閫傚簲澶у瀷椤圭洰鐨勫伐鍏?銆傚畠杩樺彲浠ヤ氦鍙夌紪璇戞垚 JavaScript銆傝繖绉嶈瑷€浣滀负鎶€鏈瑙堢増鍚戞洿骞挎硾鐨勪笘鐣屽彂甯?骞跺懡鍚嶄负 Dart銆?/p>

瀵癸紝Dart鍏跺疄鍜孴s鐨勫垵琛蜂竴鏍凤紝閮芥槸涓轰簡鎴愪负鏇村ソ鐨凧avascript锛岃€屼笖涓ら棬璇█鐨勯潰涓栨椂闂村叾瀹炲樊涓嶅锛宒art闈笘浜?011骞达紝ts闈笘浜?012骞达紝绱ч殢鍏跺悗锛沝art鏄敱Google鍥㈤槦寮€鍙戯紝ts鐨勮儗鍚庡洟闃熸槸寰蒋锛屼袱鑰呭湪涓€娈垫椂闂村唴鏄珵浜夊叧绯汇€?/p>

鍚庢潵dart鍦ㄤ笌typescript绔炰簤涓€樿皝鎵嶆槸鏇村ソ鐨刯s鈥欎腑杈撶粰浜唗s锛堣繖閲岀殑鈥滅珵浜夆€濆苟涓嶆槸鎸囪瑷€鍜屾妧鏈眰闈笂鐨勭珵浜夛紝涔熸湁鍙兘鍟嗕笟鍜屽競鍦哄眰闈笂鐨勭珵浜夛級鍙嶆鏈€缁堢殑缁撴灉鏄疶s濡備粖浣滀负鏇村ソ鐨刯s锛屽凡缁忓湪鍚勫ぇ鍓嶇妗嗘灦涓婃櫘鍙婏紝react锛寁ue鍜孉ngular閮藉湪绉瀬鐨勬敮鎸佷笌浣跨敤ts銆?/p>

鑰宒art鍦ㄦ矇闈欎簡涓€娈垫椂闂村悗鍒欏€熷姪Flutter鍜岃法骞冲彴鐨勫叴璧凤紝鍐嶆鍑虹幇鍦ㄥ叕浼楅潰鍓嶃€?/p>

鎵€浠ワ紝鍗曚粠璇█鏈韩鏉ヨ锛屼笁鑰呮槸鏈夊緢澶氱殑鐩镐技涔嬪鐨勩€傛瘮濡俲s鍜宒art閮芥槸鍗曠嚎绋嬬殑锛宩s鍙互閫氳繃娴忚鍣ㄧ殑澶氱嚎绋嬪幓瀹屾垚闀挎椂闂磋€楁椂浠诲姟锛宒art鍙互閫氳繃骞冲彴閫氶亾銆傛墍浠ユ垜浠潵鐪嬬湅ts鍦ㄩ缚钂欎笂鏄€庝箞鎵ц闀挎椂闂磋€楁椂浠诲姟鐨勩€?/p>

澶氱嚎绋嬫敮鎸?/h3>

骞跺彂鍜屽绾跨▼

  • 寮傛骞跺彂鏄寚寮傛浠g爜鍦ㄦ墽琛屽埌涓€瀹氱▼搴﹀悗浼氳鏆傚仠锛屼互渚垮湪鏈潵鏌愪釜鏃堕棿鐐圭户缁墽琛岋紝杩欑鎯呭喌涓嬶紝鍚屼竴鏃堕棿鍙湁涓€娈典唬鐮佸湪鎵ц銆傚锛歛ysnc/promise

  • 澶氱嚎绋嬪苟鍙戝厑璁稿湪鍚屼竴鏃堕棿娈靛唴鍚屾椂鎵ц澶氭浠g爜銆傚湪涓荤嚎绋嬬户缁搷搴旂敤鎴锋搷浣滃拰鏇存柊UI鐨勫悓鏃讹紝鍚庡彴涔熻兘鎵ц鑰楁椂鎿嶄綔锛屼粠鑰岄伩鍏嶅簲鐢ㄥ嚭鐜板崱椤裤€?/p>

HarmonyOs瀵瑰绾跨▼鐨勫紑鍙戞彁渚涗簡涓ょ鏂瑰紡锛孴askPool鍜學orker锛屽彲浠ュ畼缃戠湅瀵规瘮

绀轰緥

import Prompt from '@system.prompt'
import taskpool from '@ohos.taskpool';
import worker from '@ohos.worker';

@Entry
@Component
struct ParentComponent {
  @State number: number = 0

  build() {
    Column({ space: 20 }) {
      LoadingProgress().width(100).height(100)
      Button("setTimeout鎵ц鑰楁椂浠诲姟")
        .onClick(() => {
          longTaskWithTimeOut()
        })
      Button("taskPool鎵ц鑰楁椂浠诲姟")
        .onClick(() => {
          doWithTaskPool()
        })
      Button("worker鎵ц鑰楁椂浠诲姟")
        .onClick(() => {
          doWithTaskPool()
        })
    }.width("100%").height("100%")
  }
}

function longTask(): string {
  console.log("寮€濮嬮暱浠诲姟");
  const startTime = Date.now();
  while (Date.now() - startTime < 1000) {
  }
  console.log(`鎵ц瀹屾瘯闀夸换鍔★紝鑰楁椂${(startTime - Date.now())}ms`);
  return "ok"
}


async function longTaskWithTimeOut() {
  let result: string = await new Promise<string>((success, error) => {
    setTimeout(() => {
      success(longTask());
    }, 0);
  })
  Prompt.showToast({ message: `result is ${result}` })
}


@Concurrent
function task() {
  const startTime = Date.now();
  while (Date.now() - startTime < 1000) {
  }
  return "ok"
}

/**
 * 閫傚悎鍗曚釜瀹屾暣鐨勪换鍔?
 */
function doWithTaskPool() {
  const tmpTask = new taskpool.Task(task)
  taskpool.execute(tmpTask).then(result => {
    Prompt.showToast({ message: `Result : ${result}` })
  })
  // const promise: Promise<unknown> = taskpool.execute(tmpTask)
  // const data = await promise
}

function doWithWorker() {
  const tmpWorker = new worker.ThreadWorker(`entry/src/main/ets/workers/DemoWorker.ts`);
  tmpWorker.onmessage = result => {
    Prompt.showToast({ message: `Result : ${result}` })
  }
  // 鍚慦orker瀛愮嚎绋嬪彂閫丟et娑堟伅
  tmpWorker.postMessage({ data: "data" })
  // tmpWorker.terminate()
}taskpool.execute(tmpTask)
  // const data = await promise
}
// worker.ets
import worker, { ErrorEvent, MessageEvents, ThreadWorkerGlobalScope } from '@ohos.worker';

var workerPort: ThreadWorkerGlobalScope = worker.workerPort;
workerPort.onmessage = function (e: MessageEvents) {
  const startTime = Date.now();
  while (Date.now() - startTime < 1000) {
  }
  workerPort.postMessage("ok")
}

https://www.xamrdz.com/backend/3vr1945710.html

相关文章: