scala快速一览
println("hello world");
val x = +;
println(x);
//val 不允许再次赋值
//x = 3;
//变量var
var xx = x;
xx = xx+;
println(xx);
//blocks
{
var xxx = ;
println(xxx);
}
//Functions
//Functions are expressions that take parameters.
(x:Int)=>x+;
//name Functions
//use var
var addOne = (x:Int)=>x+;
println(addOne());
addOne = (x:Int) => x+;
println(addOne());
//use val
val addOneVal = (x:Int)=>x+;
println(addOneVal());
//error reassignment to val
// addOneVal = (x:Int)=>x+2;
// println(addOneVal(12));
//method
//method Methods look and behave very similar to functions,
//but there are a few key differences between them.
//Methods are defined with the def keyword.
//def is followed by a name, parameter lists,
//a return type, and a body.
def add(x:Int):Int = x;
println(add());
def addTwo(x:Int)(xxx:Int):Int =
{
//return not must need
return xxx+x;
}
println(addTwo()());
def addTwo(x:Int,y:Int)(xxx:Int) : Int =
{
//return not must need
//
//The last expression in the body is the method’s return value.
//Scala does have a return keyword, but it’s rarely used.
x+y-xxx;
}
println(addTwo(,)());
//no param
def name : String = {
System.getProperty("user.name");
}
println("name="+name);
class Test(name:String,money:Double)
{
def call(youName:String):Unit=
{
println("name="+name);
println("money="+money);
println("youName="+youName);
println("\n");
}
}
var test1 = new Test("kk1",10D);
test1.call("zzz1");
val test2 = new Test("kk2",10D);
test2.call("zzz2");
//reassignment to val
//test2 = test1;
var test3 = new Test("kk3",10D);
test3.call("zzz3");
test3 = test2;
test3.call("zzz3");
test3 = test1;
test3.call("zzz3")
//case class
//Scala has a special type of class called a “case” class.
//By default,case classes are immutable and compared by value
case class Point(x:Int,y:Int)
val p1 = new Point(,)
val p2 = new Point(,)
var p3 = new Point(,)
if(p1==p2)
println("p1 eq p2 "+"p1="+p1+" p2="+p2)
else
println("p1 neq p2 "+"p1="+p1+" p2="+p2)
if(p1==p3)
println("p1 eq p3 "+"p1="+p1+" p3="+p3)
else
println("p1 neq p3 "+"p1="+p1+" p3="+p3)
case class Point2(x:Int,y:Int)
{
def test(xx:Int):Int=
{
println("here")
x+xx
}
}
val pp2 = new Point2(,);
println(pp2.test());
//Objects are single instances of their own definitions.
//You can think of them as singletons of their own classes.
//定义的实例是对象
object Test2
{
def call(x:Int):Int = x+
}
println(Test2.call());
val ttt = Test2.call()
println(ttt)
var ttt2: Int = Test2.call()
println(ttt2);
val ttt3: Int = Test2.call()
println(ttt3);
//Traits are types containing certain fields and methods
trait Interface
{
val xx = (x:Int)=>x+
var xxx = (x:Int)=>x+
var xi:Int =
def test(x:Int,y:Int):Unit =
{
println(x)
}
}
//trait Interface is abstract; cannot be instantiated
//val i = new Interface();
// val i = new Interface();
// println(i.test(11,11));
println("\n\n");
class DefaultIntereface extends Interface
{
override def test(x:Int,y:Int) : Unit =
{
println(xx(x));
println(xxx(x));
xxx = xx;
xi = x;
println(x);
}
}
val i = new DefaultIntereface();
i.test(,);
println("\n\n");
//value k is not a member of
//ScalaFiddle.this.DefaultIntereface
// if(i.k==null)
// println(i.test(11,11))
i.test(,)
println("\n\n");
println(i.xi)
i.xi = ;
println(i.xi)
//The main method is an entry point of a program
object Main
{
def main(args:Array[String]):Unit=
{
println("this is Main")
}
}
Main.main(null);
val和var的区别
val是值,不可变
var是变量,可变
函数只是过程,函数定义语法
[var-val] = (paramName:paramType,paramName:paramType) => process
方法,方法和函数类型,有以下不用
使用def 关键字定义,有名字,有参数列表,有返回值,scala有return关键字,但它不是必须的,默认最后一行的计算结果就是返回值
def methodName(paramName:paramType) : returnType ={
}
Class
class ClassName(paramName:paramType){
//method
}
//make an instance of a class
val greeter = new Greeter("Hello, ", "!")
case class
case class是特别的类型,它不可变,通过值比较
Objects 对象是根据定义生成的实例,语法如下
object ObjectName
{
//field
//method
}
对象不可new
traits,traits包含方法和字段
traits traitsName
{
//field
//method,方法可以有默认实现
//如果实现traits的实现重载traits里面的方法,必须要有override
}
scala快速一览的更多相关文章
- Scala快速入门到精通 视频教程 百度云网盘下载地址
Scala快速入门到精通 视频教程 百度云网盘下载地址 Scala快速入门到精通 下载地址链接:https://pan.baidu.com/s/1bqGIKyF 密码:ojwd
- ASP.NET Core on K8S学习初探(2)K8S基本概念快速一览
在上一篇<单节点环境搭建>中,通过Docker for Windows在Windows开发机中搭建了一个单节点的K8S环境,接下来就是动人心弦的部署ASP.NET Core API到K8S ...
- scala快速入门之文档注释
scala快速入门之文档注释 1.在项目栏的目录树中找到该源码,右击点击Show in Explorer, 即可找到该源码的本地路径,在路径中输入cmd 2.执行scaladoc -d 生成文档注释 ...
- Scala快速入门 - 基础语法篇
本篇文章首发于头条号Scala快速入门 - 基础语法篇,欢迎关注我的头条号和微信公众号"大数据技术和人工智能"(微信搜索bigdata_ai_tech)获取更多干货,也欢迎关注我的 ...
- Scala快速入门(四)——继承、接口
Scala快速入门(四)--继承.接口 一.继承 1.继承的概念:省略 2.模板: class Person(n:String,a:Int) { var name:String=n var age:I ...
- Scala - 快速学习05 - 数据结构
1- 数组(Array) 数组一般包括定长数组和变长数组. 可以不指明数组类型,Scala会自动根据提供的初始化数据来推断出数组的类型. 在Scala中,对数组元素的应用,是使用圆括号,而不是方括号. ...
- Spark(七) -- Scala快速入门
Scala作为Spark的开发语言,想要成为Spark高手,精通Scala是必须要走的一条路 然后一门语言并不是你想精通就能够精通的,更何况是Scala这种面向对象又面向函数的编程语言,个人觉得其学习 ...
- Scala快速概览
IDEA工具安装及scala基本操作 目录 一. 1. 2. 3. 4. 二. 1. 2. 3. 三. 1. 2. 3. 4. 5. 6. 7. 四. 1. (1) (2) (3) (4) (5) ( ...
- <Araxis Merge>快速一览文件的比较与合并
重要的文件比较与合并特性在下面都指出了.对每个特性的说明性内容在下面可以找到. 注意:只有双向的比较/合并被展示了,专业版的Merge还支持三向的比较/合并. 1.文件夹比较按钮 单击这个工具栏按钮会 ...
随机推荐
- php解析url并得到url中的参数及获取url参数
<?php $url = 'http://www.baidu.com/index.php?m=content&c=index&a=lists&catid=6&ar ...
- aircrack 破解wifi密码
分享一个用aircrack破解wifi密码的步骤: 1.新建一个终端 airmon-ng check kill airmon-ng start wlan0 airodump-ng wlan0mon 此 ...
- pytest.4.Fixture
From: http://www.testclass.net/pytest/fixture/ 我们可以简单的把Fixture理解为准备测试数据和初始化测试对象的阶段. 一般我们对测试数据和测试对象的管 ...
- 使用RetionalRose根据现有的java工程逆向生成类图
1.进入RetionalRose选择J2EE模板 2.在菜单栏选择tools->java/j2EE->reverse engineer 3.编辑路径Edit CLASSPATH选择要生成类 ...
- 使用Google cardboard 2的一些软件
最近入手cardboard2,FQ尝试了一些软件,特别分享,给大家提供一些方便. 链接:http://pan.baidu.com/s/1slehilZ 密码:b49h
- levenshtein函数
Levenshtein算法已在部分DBMS中实现. (例如:PostgreSQL:http://www.postgresql.org/docs/9.1/Static/fuzzystrmedi.html ...
- ACM主要算法
ACM主要算法ACM主要算法介绍 初期篇 一.基本算法(1)枚举(poj1753, poj2965)(2)贪心(poj1328, poj2109, poj2586)(3)递归和分治法(4)递推(5)构 ...
- 廖雪峰Java5集合-2List-2编写equals方法
List是一种有序链表: List内部按照放入元素的先后顺序存放 每个元素都可以通过索引确定自己的位置 boolean contains(Object o) 是否包含某个元素 int indexOf( ...
- win10使用4G 模块RNDIS模式上网
Windons使用RNDIS模式上网步骤 Chapter 1 模块端配置 1模块设置为RNDIS模式 1. 以EC20CEFAG模块为例 2. 命令如下: 1) ...
- linux system()函数详解
system(3) - Linux man page Name system - execute a shell command Synopsis #include <stdlib.h> ...