scala基础语法_Scala基础知识和语法 Scala编程教程

scala基础语法

After installing and knowing about Scala, it time to hop on to some basic concepts that are important in Scala. You will use them many times in your Scala development carrier.

安装并了解了Scala之后 ,该介绍一下Scala 中重要的一些基本概念了 。 您将在Scala开发载体中多次使用它们。

一些基本的Scala概念 (Some basic Scala concepts)

  1. OBJECT:

    目的:

    An object is an instance of a class. it has states and behavior. For Example, A car has states: color, model, number of seats and behaviors: driving, speed.

    对象是类的实例。 它具有状态和行为。 例如,一辆汽车具有以下状态:颜色,型号,座位数和行为:行驶,速度。

  2. Class:

    类:

    A class is a blueprint that defines the states and behaviors related to it.

    类是定义其相关状态和行为的蓝图。

  3. Methods:

    方法:

    It defines the behavior of the objects. It contains logic that are to be performed on the variables.

    它定义了对象的行为。 它包含对变量执行的逻辑。

  4. FIELDS:

    领域:

    It defines the states of objects. These are variables that a class uses to store its data.

    它定义了对象的状态。 这些是类用于存储其数据的变量。

  5. CLOSURE:

    关闭:

    It is a function whose value depends on the value of multiple variables that are declared outside the function. These variables must be either parameters or in the scope of the variable.

    它是一个函数,其值取决于在函数外部声明的多个变量的值。 这些变量必须是参数或在变量范围内。

  6. TRAITS:

    特质:

    Traits are just like classes they have fields and methods but multiple traits can be used together(inherited) to increase the functionality of a class.

    特性就像类一样,它们具有字段和方法,但是可以将多个特性一起使用(继承)以增加类的功能。

句法 (Syntax)

There is some basic syntax that needs to be addressed before writing our first program to make sure it compiles and run flawlessly.

在编写我们的第一个程序之前,需要解决一些基本语法,以确保其可以正确编译和运行。

  • No need of semicolon ; (it optional).

    不需要分号; (可选)。

  • Scala does not require any datatype or return type while initialization. Use var for variable and def for function declaration instead.

    初始化时,Scala不需要任何数据类型或返回类型。 使用var表示变量,而def表示函数声明。

  • Naming in Scala, Scala is case sensitive. And valid characters are alphabets, digits and _.

    在Scala中命名时,Scala区分大小写。 有效字符是字母,数字和_ 。

    • Class: use camel case with the first letter of every word capital. Example, MyFirstClass类:使用驼峰式大小写,每个单词的首字母大写。 例如,MyFirstClass
    • Function: use camel case with the first letter of every word (except the first word) capital. Example, myFirstFunction. 功能:使用驼峰式大小写,每个单词(第一个单词除外)的首字母大写。 例如,myFirstFunction。
    • Filename: it must be the same as the name of the class.文件名:必须与类名相同。
  • Main Function in Scala: The main function is defined as,

    Scala中的主要功能主要功能定义为:

    def main(args: Array[String])


第一个Scala程序 (First Scala program)

Programming in Scala is easy and you can write your program in either way

在Scala中进行编程很容易,您可以用任何一种方式编写程序

  1. Using terminal (interactive method)

    使用终端(交互式方法)

  2. Using Notepad (Script method)

    使用记事本(脚本方法)

1) Using terminal (Interactive Method)

1)使用终端(交互式方法)

In this method, we code directly in the terminal and execute the code by hitting enter. Multiline code separated by ;.

在这种方法中,我们直接在终端中编码,然后按Enter键执行代码。 用分隔的多行代码; 。

Steps to write code in the terminal

在终端中编写代码的步骤

Step 1: Open Terminal → Type command

步骤1:打开终端→输入命令

    \>scala 

Step 2: Your Terminal will display the version of Scala with some instructions.

步骤2:您的终端将显示Scala的版本以及一些说明。

Step 3: Now, a > icon will appear. Code your stuff there and hit enter to view the output.

步骤3:现在,将出现一个>图标。 在此处编码您的内容,然后按Enter键以查看输出。

2) Using Notepad (Script Method)

2)使用记事本(脚本方法)

In this method, we will code in a file and then compile it and run to see the output. This method is generally preferred as coding is easy and the saved copy is maintained in case of failure.

在这种方法中,我们将在文件中进行编码,然后对其进行编译并运行以查看输出。 通常首选此方法,因为它易于编码,并且在出现故障的情况下可以保留已保存的副本。

Steps to write the code in Notepad

在记事本中编写代码的步骤

Step 1: Open your notepad and code your Scala program

第1步:打开记事本并对Scala程序进行编码

object HelloWorld {
	def main(args: Array[String]) {
		println("Hello, world! This is my first Scala Program"); 
	}
}

Step 2: Save your code with an extension ".scala" : myFile.scala.

步骤2:使用扩展名“ .scala”保存代码: myFile.scala 。

Step 3: Open your terminal and type in the following command and instead of myFile.scala enter the name your file.

步骤3:打开终端并输入以下命令,而不是myFile.scala输入文件名。

    \> scalac HelloWorld.scala
    \> scala HelloWorld

Step 4: View your output as you require.

步骤4:根据需要查看输出。

In my case output is, Hello, world! This is my first Scala Program

就我而言,输出是世界! 这是我的第一个Scala程序

翻译自: https://www.includehelp.com/scala/basics-and-syntax.aspx

scala基础语法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值