博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
scala编程_Scala编程语言简介
阅读量:2533 次
发布时间:2019-05-11

本文共 6449 字,大约阅读时间需要 21 分钟。

scala编程

Scala is a programming language that integrates the object oriented and functional language features. This programming language was developed by Martin Odersky and released in 2003. It has been used to develop several scalable concurrent applications in the recent years.

Scala是一种编程语言,集成了面向对象和功能语言的功能。 该编程语言由Martin Odersky开发并于2003年发布。近年来,它已被用于开发多个可伸缩的并发应用程序。

Scala功能 (Scala Features)

The features of Scala are as listed below;

Scala的功能如下所示;

  • Object Oriented: Scala is pure object oriented language where each value is treated as an object. Behaviours and classes of the object are described by classes and traits. Multiple Inheritance is supported by mixin based composition and extending classes by subclassing.

    面向对象 :Scala是纯面向对象的语言,其中每个值都被视为一个对象。 对象的行为和类别由类别和特征描述。 基于mixin的合成支持多继承,并通过子类扩展类。
  • Functional: Scala is a functional programming language where each function is a value. Every value is an object so each function is an object. Scala supports Nested and Higher order functions and provisions for defining anonymous functions.

    功能性: Scala是一种功能性编程语言,其中每个功能都是一个值。 每个值都是一个对象,因此每个函数都是一个对象。 Scala支持嵌套和高阶函数以及用于定义匿名函数的规定。
  • Statically Typed: Scala enforces type system that provides constraints and abstractions to be used in a safe and coherent manner in other words Scala is a strongly typed language. The type system support includes variance annotations, generic classes, upper and lower bounds, explicitly typed self references, views and polymorphic methods. Local Type Inference mechanism ensures that there is no need to annotate the program with redundant type information. All these together forms a powerful basis for safe reuse of programming abstractions.

    静态类型化: Scala强制执行类型系统,该系统提供安全和连贯的方式使用的约束和抽象,换句话说,Scala是一种强类型化语言。 类型系统支持包括方差注释,通用类,上限和下限,显式键入的自引用,视图和多态方法。 本地类型推断机制可确保无需使用冗余类型信息来注释程序。 所有这些共同构成了安全地重用编程抽象的强大基础。
  • Extensible: Scala provides language mechanism combinations that are unique and hence easier to integrate new language constructs in the form of libraries. Closures are automatically constructed upon the expected type and any method can be used as infix or postfix operators. New statements can be facilitated without extending the syntax and macro like meta programming facilities.

    可扩展: Scala提供了独特的语言机制组合,因此更易于以库的形式集成新的语言结构。 闭包是根据预期的类型自动构建的,任何方法都可以用作infix或postfix运算符。 无需扩展语法和宏(如元编程工具)就可以促进新语句的生成。
  • Runs on JVM: Scala runs on Java Virtual Machine(JVM) and when a scala program is compiled, byte code is generated that is executed by JVM. Both Scala and Java share a common platform and hence java programs can be converted to Scala.

    在JVM运行: Scala在Java虚拟机(JVM)上运行,并且在编译Scala程序时,将生成由JVM执行的字节码。 Scala和Java共享一个公共平台,因此可以将Java程序转换为Scala。
  • Executes Java Code: Scala can use all the classes of Java SDK, custom java classes or Java open source projects.

    执行Java代码: Scala可以使用Java SDK的所有类,自定义Java类或Java开源项目。

Scala和Java (Scala and Java)

Scala provides some of the extra features that differ from Java. They are;

Scala提供了一些与Java不同的额外功能。 他们是;

  1. Concurrency Support: Support for multiple cpu/core utilization out of the box enabling faster execution of programs unlike traditional programming languages.

    并发支持 :支持开箱即用的多种CPU /内核,与传统编程语言不同,可以更快地执行程序。
  2. Closures: Closure is a function whose return value depends on the value of one or more variables defined outside this function.

    Closures :Closure是一个函数,其返回值取决于此函数外部定义的一个或多个变量的值。
  3. Traits: Trait consists of a method and field definitions that can be reused by mixing them into classes.

    特性 :特性包括方法和字段定义,可以通过将它们混合到类中来重用。
  4. Nested Functions: A Function defined inside another function is termed as a Nested function and is supported in Scala.

    嵌套函数 :在另一个函数内部定义的函数称为嵌套函数,在Scala中受支持。
  5. Domain Specific Language support: Enables users to write their own language specifications and implement using Scala.

    域特定语言支持 :使用户能够编写自己的语言规范并使用Scala实施。
  6. Type Inference: Automatic detection of data types even though not specified by the user.

    类型推断 :即使用户未指定,也自动检测数据类型。
  7. All functions are object: It supports all standard OOPS concepts and makes functions easy to reuse.

    所有功能都是对象 :它支持所有标准OOPS概念,并使功能易于重用。
  8. All types are Objects: Everything is an object in Scala unlike in Java.

    所有类型都是对象 :与Java不同,Scala中的所有对象都是对象。
  9. Supports REPL (Read Evaluate Print and Loop): Allows us to type the programs on the fly in a shell and get back immediate output.

    支持REPL(读取评估打印和循环) :允许我们在shell中即时键入程序并立即返回输出。

Scala Hello World程序 (Scala Hello World Program)

Let us consider a simple hello world program and understand the basic structure of a scala program. This is just to get started and will go into setup and installation procedures in detail in the next article.

让我们考虑一个简单的hello world程序并了解scala程序的基本结构。 这只是开始,将在下一篇文章中详细介绍安装和安装过程。

scala> object HelloWorld {| def main(args: Array[String]) {| println("Hello World!!")| }| }

object is the keyword, HelloWorld is the name of the object that will be created.

object是关键字, HelloWorld是将要创建的对象的名称。

Scala program execution starts with main method so the main method is defined by passing string array as an argument.

Scala程序的执行从main方法开始,因此main方法是通过将字符串数组作为参数来定义的。

def is a keyword indicating that we are defining a method whose name is “main”. We use println to print “Hello World”.

def是一个关键字,指示我们正在定义一个名称为“ main”的方法。 我们使用println打印“ Hello World”。

关于Scala程序的要点 (Key points about Scala Program)

  • Scala Programs are case sensitive HelloWorld is different from helloWorld

    Scala程序区分大小写HelloWorld与helloWorld不同
  • First letter of the Class Name should be in uppercase.

    类名的首字母应大写。
  • File name should be exactly same as the object name

    文件名应与对象名完全相同
  • Method names should begin with a lowercase.

    方法名称应以小写字母开头。

After typing the above program in scala shell run the program we get the message “defined object HelloWorld”

在scala shell中键入上述程序后,运行该程序,我们将收到消息“已定义对象HelloWorld”

Now run the HelloWorld program as;

现在,以以下方式运行HelloWorld程序:

scala>HelloWorld.main(null)Hello World!!

Here we invoke the main method of the HelloWorld object.

在这里,我们调用HelloWorld对象的main方法。

Scala框架 (Scala Frameworks)

Scala is used a lot these days especially in enterprise applications. That’s why there are frameworks getting developed on top of Scala programming language. Some of the popular frameworks include;

如今,Scala已被大量使用,尤其是在企业应用程序中。 这就是为什么在Scala编程语言之上开发框架的原因。 一些流行的框架包括:

  • Play framework

    播放框架
  • Bowler framework

    礼帽框架
  • Lift framework

    升降框架

That’s all for brief introduction of Scala Programming Language, we will look into installation and Scala language core features in coming posts.

以上就是对Scala编程语言的简要介绍,我们将在以后的文章中探讨安装和Scala语言的核心功能。

翻译自:

scala编程

转载地址:http://yuqzd.baihongyu.com/

你可能感兴趣的文章
Ubuntu菜鸟入门(五)—— 一些编程相关工具
查看>>
valgrind检测linux程序内存泄露
查看>>
Hadoop以及组件介绍
查看>>
1020 Tree Traversals (25)(25 point(s))
查看>>
第一次作业
查看>>
“==”运算符与equals()
查看>>
单工、半双工和全双工的定义
查看>>
Hdu【线段树】基础题.cpp
查看>>
时钟系统
查看>>
BiTree
查看>>
5个基于HTML5的加载动画推荐
查看>>
水平权限漏洞的修复方案
查看>>
静态链接与动态链接的区别
查看>>
Android 关于悬浮窗权限的问题
查看>>
如何使用mysql
查看>>
linux下wc命令详解
查看>>
敏捷开发中软件测试团队的职责和产出是什么?
查看>>
在mvc3中使用ffmpeg对上传视频进行截图和转换格式
查看>>
python的字符串内建函数
查看>>
Spring - DI
查看>>