“ddac5c107942d9584a9f55701aad405b57618726”上不存在“drivers/media/i2c/saa7115.c”
未验证 提交 b5aedf06 编写于 作者: O openharmony_ci 提交者: Gitee

!22671 cherry-pick 更新ArkTS文档

Merge pull request !22671 from huoqingyi/cherry-pick-1692675518
...@@ -766,7 +766,7 @@ class Point { ...@@ -766,7 +766,7 @@ class Point {
x: number = 0 x: number = 0
y: number = 0 y: number = 0
} }
let p: Point = {42 ,42} let p: Point = {x: 42, y: 42}
``` ```
### 字段 ### 字段
...@@ -868,11 +868,11 @@ class Rectangle { ...@@ -868,11 +868,11 @@ class Rectangle {
} }
``` ```
实例方法需要在类的实例上调用 必须通过类的实例调用实例方法
```typescript ```typescript
let r = new Rectangle(10, 10) let square = new Rectangle(10, 10)
console.log(square.calculateArea()) // output: 100 console.log(square.calculateArea()) // 输出:100
``` ```
#### 静态方法 #### 静态方法
...@@ -883,7 +883,7 @@ console.log(square.calculateArea()) // output: 100 ...@@ -883,7 +883,7 @@ console.log(square.calculateArea()) // output: 100
所有实例都可以访问静态方法。 所有实例都可以访问静态方法。
要调用静态方法,需要使用类名 必须通过类名调用静态方法
```typescript ```typescript
class Cl { class Cl {
...@@ -1309,7 +1309,7 @@ class Stack<Element> { ...@@ -1309,7 +1309,7 @@ class Stack<Element> {
public pop(): Element { public pop(): Element {
// ... // ...
} }
public push(e: Element) { public push(e: Element):void {
// ... // ...
} }
} }
...@@ -1402,7 +1402,7 @@ foo<number>() ...@@ -1402,7 +1402,7 @@ foo<number>()
## 空安全 ## 空安全
默认情况下,ArkTS中的所有类型都是不可为空的,因此类型的值不能为空。这类似于TypeScript的严格空值检查模式(`strictNullChecks`),但规则更严格,而且ArkTS中没有`undefined`类型 默认情况下,ArkTS中的所有类型都是不可为空的,因此类型的值不能为空。这类似于TypeScript的严格空值检查模式(`strictNullChecks`),但规则更严格。
在下面的示例中,所有行都会导致编译时错误: 在下面的示例中,所有行都会导致编译时错误:
...@@ -1454,28 +1454,47 @@ class Person { ...@@ -1454,28 +1454,47 @@ class Person {
### 可选链 ### 可选链
可选链运算符`?.` 可以在编写代码时遇到求值为null的表达式就停止运行 在访问对象属性时,如果该属性是`undefined`或者`null`,可选链运算符会返回`undefined`
```typescript ```typescript
class Person { class Person {
// ... nick : string | null = null
spouse: Person | null = null spouse ?: Person
nick: string | null = null
getSpouseNick(): string | null { setSpouse(spouse: Person) : void {
this.spouse = spouse
}
getSpouseNick(): string | null | undefined {
return this.spouse?.nick return this.spouse?.nick
} }
constructor(nick: string) {
this.nick = nick
this.spouse = undefined
}
} }
``` ```
**说明**`getSpouseNick`的返回类型必须为`string | null`,因为该方法可能返回null **说明**`getSpouseNick`的返回类型必须为`string | null | undefined`,因为该方法可能返回`null`或者`undefined`
可选链可以是任何长度的,可包含任意数量的 `?.` 运算符。 可选链可以任意长,可以包含任意数量的`?.`运算符。
在以下示例中,如果某人有配偶,且配偶有昵称,则输出是该人的配偶昵称。否则,输出为空 在以下示例中,如果一个`Person`的实例有不为空的`spouse`属性,且`spouse`有不为空的`nickname`属性,则输出`spouse.nick`。否则,输出`undefined`
```typescript ```typescript
let p: Person = ... class Person {
console.log(p?.spouse?.nick) nick : string | null = null
spouse ?: Person
constructor(nick: string) {
this.nick = nick
this.spouse = undefined
}
}
let p: Person = new Person("Alice")
console.log(p.spouse?.nick) // 打印undefined
``` ```
## 模块 ## 模块
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册