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
}