Skip to content

modify Chinese version according to English Version for num 5 and num 6 #2567

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
161 changes: 152 additions & 9 deletions _zh-cn/overviews/scala3-book/taste-hello-world.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,43 @@ permalink: "/zh-cn/scala3/book/:title.html"
---


Scala 3 “Hello, world!” 例子展示如下。
> **提示**:在以下示例中,尝试选择您喜欢的 Scala 版本。
> <noscript>><span style="font-weight: bold;">信息</span>:JavaScript 当前已禁用,代码选项卡仍将正常工作,但首选项不会被记住。</noscript>

## 你的第一个 Scala 程序

Scala “Hello, world!” 例子展示如下。
首先,把以下代码写入 _Hello.scala_:

<!-- Display Hello World for each Scala Version -->
{% tabs hello-world-demo class=tabs-scala-version %}

{% tab 'Scala 2' for=hello-world-demo %}
```scala
object hello {
def main(args: Array[String]) = {
println("Hello, World!")
}
}
```
> 代码中,在名为 `hello` 的 Scala `object` 中,我们定义了一个名称为 `main` 的方法。
> 在 Scala 中 `object` 类似 `class`,但定义了一个可以传递的单例实例。
> `main` 用名为 `args` 的输入参数,该参数必须是 `Array[String]` 类型(暂时忽略 `args`)。

{% endtab %}

{% tab 'Scala 3' for=hello-world-demo %}
```scala
@main def hello() = println("Hello, world!")
@main def hello() = println("Hello, World!")
```
> 代码中, `hello` 是方法。
> 它使用 `def` 定义,并用 `@main` 注释的手段把它声明为“main”方法。
> 使用 `println` 方法,它在标准输出 (STDOUT)中打印了 `"Hello, world!"` 字符串。

代码中, `hello` 是方法。
它使用 `def` 定义,并用 `@main` 注释的手段把它声明为“main”方法。
使用 `println` 方法,它在标准输出 (STDOUT)中打印了 `"Hello, world!"` 字符串。
{% endtab %}

{% endtabs %}
<!-- End tabs -->

下一步,用 `scalac` 编译代码:

Expand All @@ -32,15 +59,32 @@ $ scalac Hello.scala

如果你是从 Java 转到 Scala,`scalac` 就像 `javac`,所以该命令会创建几个文件:

<!-- Display Hello World compiled outputs for each Scala Version -->
{% tabs hello-world-outputs class=tabs-scala-version %}

{% tab 'Scala 2' for=hello-world-outputs %}
```bash
$ ls -1
hello$.class
hello.class
hello.scala
```
{% endtab %}

{% tab 'Scala 3' for=hello-world-outputs %}
```bash
$ ls -1
Hello$package$.class
Hello$package.class
Hello$package.tasty
Hello.scala
hello$package$.class
hello$package.class
hello$package.tasty
hello.scala
hello.class
hello.tasty
```
{% endtab %}

{% endtabs %}
<!-- End tabs -->

与 Java 一样,_.class_ 文件是字节码文件,它们已准备好在 JVM 中运行。

Expand All @@ -55,4 +99,103 @@ Hello, world!

> 在 [Scala 工具][scala_tools] 章节中可以找到 sbt 和其他使 Scala 开发更容易的工具相关的更多信息。

## 要求用户输入

在下一个示例中,让我们在问候用户之前询问用户名!

有几种方法可以从命令行读取输入,但一种简单的方法是使用
_scala.io.StdIn_ 对象中的 `readline` 方法。要使用它,您需要先导入它,如下所示:

{% tabs import-readline %}
{% tab 'Scala 2 and 3' for=import-readline %}
```scala
import scala.io.StdIn.readLine
```
{% endtab %}
{% endtabs %}

为了演示其工作原理,让我们创建一个小示例。将此源代码放在名为 _helloInteractive.scala_ 的文件里:

<!-- Display interactive Hello World application for each Scala Version -->
{% tabs hello-world-interactive class=tabs-scala-version %}

{% tab 'Scala 2' for=hello-world-interactive %}
```scala
import scala.io.StdIn.readLine

object helloInteractive {

def main(args: Array[String]) = {
println("Please enter your name:")
val name = readLine()

println("Hello, " + name + "!")
}

}
```
{% endtab %}

{% tab 'Scala 3' for=hello-world-interactive %}
```scala
import scala.io.StdIn.readLine

@main def helloInteractive() =
println("Please enter your name:")
val name = readLine()

println("Hello, " + name + "!")
```
{% endtab %}

{% endtabs %}
<!-- End tabs -->

在此代码中,我们将 `readLine` 的结果保存到一个名为 `name` 的变量中,然后
使用字符串上的 `+` 运算符将 `“Hello, ”` 与 `name` 和 `"!"` 连接起来,生成单一字符串值。

> 您可以通过阅读[变量和数据类型](/zh-cn/scala3/book/taste-vars-data-types.html)来了解有关使用 `val` 的更多信息。

然后使用 `scalac` 编译代码:

```bash
$ scalac helloInteractive.scala
```

然后用 `scala helloInteractive` 运行它,这次程序将在询问您的名字后暂停并等待,
直到您键入一个名称,然后按键盘上的回车键,如下所示:

```bash
$ scala helloInteractive
Please enter your name:
```

当您在提示符下输入您的姓名时,最终的交互应如下所示:

```bash
$ scala helloInteractive
Please enter your name:
Alvin Alexander
Hello, Alvin Alexander!
```

### 关于导入的说明

正如您在此应用程序中看到的,有时某些方法或我们稍后将看到的其他类型的定义不可用,
除非您使用如下所示的 `导入` 子句:

{% tabs import-readline-2 %}
{% tab 'Scala 2 and 3' for=import-readline-2 %}
```scala
import scala.io.StdIn.readLine
```
{% endtab %}
{% endtabs %}

导入可通过多种方式帮助您编写代码:
- 您可以将代码放在多个文件中,以帮助避免混乱,并帮助导航大型项目。
- 您可以使用包含有用功能的代码库,该库可能是由其他人编写
- 您可以知道某个定义的来源(特别是如果它没有写入当前文件)。

[scala_tools]: {% link _zh-cn/overviews/scala3-book/scala-tools.md %}
33 changes: 30 additions & 3 deletions _zh-cn/overviews/scala3-book/taste-repl.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,46 +16,73 @@ permalink: "/zh-cn/scala3/book/:title.html"
Scala REPL(“Read-Evaluate-Print-Loop”)是一个命令行解释器,您可以将其用作“游乐场”区域来测试 Scala 代码。
你可以通过运行 `scala` 或 `scala3` 命令来启动一个 REPL 会话,具体取决于您在操作系统命令行中的安装,您将看到如下所示的“欢迎”提示:

{% tabs command-line class=tabs-scala-version %}

{% tab 'Scala 2' for=command-line %}
```bash
$ scala
Welcome to Scala {{site.scala-version}} (OpenJDK 64-Bit Server VM, Java 1.8.0_342).
Type in expressions for evaluation. Or try :help.

scala> _
```
{% endtab %}

{% tab 'Scala 3' for=command-line %}
```bash
$ scala
Welcome to Scala 3.0.0 (OpenJDK 64-Bit Server VM, Java 11.0.9).
Type in expressions for evaluation.
Or try :help.
Welcome to Scala {{site.scala-3-version}} (1.8.0_322, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.

scala> _
```
{% endtab %}

{% endtabs %}

REPL 是一个命令行解释器,所以它就在那里等着你输入一些东西。
现在您可以输入 Scala 表达式来查看它们是如何工作的:

{% tabs expression-one %}
{% tab 'Scala 2 and 3' for=expression-one %}
````
scala> 1 + 1
val res0: Int = 2

scala> 2 + 2
val res1: Int = 4
````
{% endtab %}
{% endtabs %}

如输出所示,如果您不为表达式的结果分配变量,REPL 会为您创建名为 `res0`、`res1` 等的变量。
您可以在后续表达式中使用这些变量名称:

{% tabs expression-two %}
{% tab 'Scala 2 and 3' for=expression-two %}
````
scala> val x = res0 * 10
val x: Int = 20
````
{% endtab %}
{% endtabs %}

请注意,REPL 输出还显示了表达式的结果。

您可以在 REPL 中运行各种实验。
这个例子展示了如何创建然后调用一个 `sum` 方法:

{% tabs expression-three %}
{% tab 'Scala 2 and 3' for=expression-three %}
````
scala> def sum(a: Int, b: Int): Int = a + b
def sum(a: Int, b: Int): Int

scala> sum(2, 2)
val res2: Int = 4
````
{% endtab %}
{% endtabs %}

如果您更喜欢基于浏览器的游乐场环境,也可以使用 [scastie.scala-lang.org](https://scastie.scala-lang.org)。

Expand Down