println("hello world");val x = 1+1;println(x);//val 不允许再次赋值 //x = 3;//变量varvar xx = x;xx = xx+1;println(xx);//blocks{ var xxx = 12; println(xxx);}//Functions//Functions are expressions that take parameters.(x:Int)=>x+1;//name Functions//use varvar addOne = (x:Int)=>x+1;println(addOne(12));addOne = (x:Int) => x+2;println(addOne(12));//use valval addOneVal = (x:Int)=>x+1;println(addOneVal(12));//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(12));def addTwo(x:Int)(xxx:Int):Int = { //return not must need return xxx+x;}println(addTwo(12)(13));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(12,12)(13));//no paramdef 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 valuecase class Point(x:Int,y:Int)val p1 = new Point(1,2)val p2 = new Point(1,3)var p3 = new Point(1,2)if(p1==p2)println("p1 eq p2 "+"p1="+p1+" p2="+p2)elseprintln("p1 neq p2 "+"p1="+p1+" p2="+p2)if(p1==p3)println("p1 eq p3 "+"p1="+p1+" p3="+p3)elseprintln("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(12,13);println(pp2.test(100));//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+100}println(Test2.call(100));val ttt = Test2.call(100)println(ttt)var ttt2: Int = Test2.call(100)println(ttt2);val ttt3: Int = Test2.call(100)println(ttt3);//Traits are types containing certain fields and methodstrait Interface{ val xx = (x:Int)=>x+1 var xxx = (x:Int)=>x+2 var xi:Int = 0 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(1,1); println("\n\n"); //value k is not a member of //ScalaFiddle.this.DefaultIntereface// if(i.k==null)// println(i.test(11,11)) i.test(1,1)println("\n\n");println(i.xi) i.xi = 12; 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 }
posted on 2018-07-15 22:22 阅读( ...) 评论( ...)