Package Cn.dowhatyouwant.codes/** * Created by Dengni on 2016/8/31. * Scala is a dynamic language, static type, because difficult, so easy to use * spark too good everyone should go to learn, use * Learning Spark preferred Scala Python * based on JVM FP + oo * and Java Interop * Interpreter ==scala Shell * val var * var h:string, after assignment Scala automatically infers data type * function * */object Testbegian {//function D EF Hello (name:string): string ={//define a function that returns the type String "Hello" + name//default last line is the return value, so do not add back} def Hellosc ALA (): The parentheses in front of the unit ={//parameterless function can remove println ("My name is xxxx")} def add= (x:int,y:int) => x + y//define an anonymous function, especially heavy To Def ADD2 (X:int) (y:int) = x*y//Corrie def printeverychar (c:string*) ={//Accept a series of String parameters, that is, variable parameter C.foreach (X=&G T;PRINTLN (x))} def xx (age:int = 18) ={//Call time if no parameters are passed in, use default parameter of age}//main F def main (args:array[s Tring]) {//println ("Hello there")//println (Hello ("Scala"))//helloscala//For parameterless functions, parentheses are not required at call time, recommended plus//PRI
Ntln (Add (1,2))//println (ADD2 (2) (4))//printeverychar ("A", "B") Printeverychar ("C", "D", "F")//println (XX ())//Only parameterless function can remove bracket//conditional expression If,scala default is not required semicolon//val x =2//v
Al B = if (x >1) 1 Else 0//println (b)//loop//var (n,r) = (10,0)//while (n > 0) {/R =r +n n = n-1//}//println (r)//foreach//for//for (i<-1 to) {//print (i)/
/println ()//}//for (I<-1 until 10) {//does not contain line 10, this is the difference//print (i)//println ()//} for (I <-1. If i%2 = 0) {//Print shared//println (i)///////loop expression without continue and BR for even-numbered for and if expressions Eak//val P =new a person//without a constructor the parentheses of the class can be omitted//p.name= "Hidoyouo"//print (p.name+ ":" + p.age)//val p
= When the new person ("hijack",)//The new variable prints the This is the primary constructor! println (P.age + p.name)//val p = new Person ("hijack", "F")//println (p.gender+p.name) val s = new Stud
ENT ("Hijck", "Eng")//is actually initializing all the methods and properties println (s.tostring)}
Class testbegian{//All classes by default are public}//class person{//var name:string = _//At this time a String is not to be removed//var name:s The variable system defined by Tring = "Hijack"//var automatically generates Geter &setter method//val the variable defined by age =//val only generates getter methods that conform to our concept of not being assignable. /private[this] var gender = "122"//variable can only be used within person, the object in the main method cannot invoke//}//1. Main constructor, directly following the class name, the main constructor's arguments are finally compiled into the field//2 of the class. The primary constructor executes the , all of the statement//3 in the class are executed. Assuming that the parameter declaration does not take Var or val, the declared variable is equivalent to private "this", which can only be used in the class, and class person (Name:string,val Age:int)
= Name class Person (val name:string,val age:int) {println (' This is the primary constructor! ') var gender:string = _//Auxiliary constructor name Wei this//each subordinate constructor must first invoke the existing child constructor or the primary constructor def this (Name:string,age:int,gender: String) {This (name,age) This.gender=gender}}//Inheritance//name Age is inherited, major is specific to the student class, so you need to use Val to define class S Tudent (name:string,age:int,val major:string) extends person (name, age) {println ("This is the subclass of Person,major Is "+ major)//overriding methods and fields of the parent class keep with the parent class at the same timeSame data type override def toString = "This from Student"}