scala学习记录一

Posted AI小白奋斗史

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了scala学习记录一相关的知识,希望对你有一定的参考价值。

val 与 var

val :值

  • final 一次定义,不可修改

  • val age:Int = 20;

var :变量

  • var name:String = "zhangsan"

  • 可修改

基本数据类型

  • byte/char

  • short/int/Long/Float/Double

    val e:Float=1.1f

  • boolean

函数/方法的定义

def 方法名(参数名:参数类型): 返回值类型={
// 花括号内的叫做方法体

//方法体内的最后一行为返回值,不需要使用return
}

默认参数

在函数定义时,允许指定参数的默认值

可变参数

  // 可变参数
def sum(numbers:Int*)={
var result = 0;
for(number <- numbers){
result += number;
}
result;
}

循环表达式

  • to 左闭右闭, 1.to(10) [1-10]

  • Range 左闭右开,Range(1,10,1) [1-9]

  • until

for(i <- 1 to 10 if i % 2==0 ){
println(i)
}


var courses = Array("Hadoop","SQL", "Storm","Scala");
for(course <- courses){
println(course)
}
// 调用println 打印数组中的元素
courses.foreach(c_name => println(c_name))

var (num,sum) = (100,0)
while (num>0){
sum += num
num -= 1
}

面向对象

类的定义和使用

  • 类的定义和使用

object SimpleObjectApp {
def main(args: Array[String]): Unit = {
val people = new People();
people.name = "Messi"
println(people.name + " ... " + people.age)

println("Invoke eat method: "+people.eat)
people.watchFootball("Barcelona ")
}
}

class People{
// 定义数学,val只生成get方法,var生成set,get方法
var name:String = "";
val age:Int = 10;

private [this] val gender = "male" // 只能class内使用

def printInfo()={
println("gender: "+gender)
}

def eat():String={
name + " Eat..."
}

def watchFootball(teamName:String)={
println(name + " is wathcing match of "+teamName )
}
}

构造器

package org.example.course04

object ConstructorApp {
def main(args: Array[String]): Unit = {
val person = new Person("zhangsan", 30)
println(person.name + " : " + person.age + " : " + person.school)

val person2 = new Person("pk", 19,"M")
println(person2.name +
" : " + person2.age
+ " : " + person2.school
+ " : "+person2.gender)
}
}

// 主构造器
class Person(val name: String, val age: Int) {

println("Person enter....")
val school = "ustc"
var gender :String = _

// 附属构造器
def this(name:String,age:Int,gender:String){
this(name,age) // 附属构造器的第一行必须要调用主构造器或者其他附属构造器
this.gender = gender
}
println("Person Constructor leave....")
}

继承

package org.example.course04

object ConstructorApp {
def main(args: Array[String]): Unit = {
val student = new Student("PK",18,"Math")
println(student.name + ": " + student.major)
}
}

// 主构造器
class Person(val name: String, val age: Int) {

println("Person Constructor enter....")
val school = "ustc"
var gender :String = _

// 附属构造器
def this(name:String,age:Int,gender:String){
this(name,age) // 附属构造器的第一行必须要调用主构造器或者其他附属构造器
this.gender = gender
}
println("Person Constructor leave....")
}

class Student(name:String,age:Int,var major:String) extends Person(name,age){
println("Person Student enter....")

println("Person Student leave....")
}

重写

package org.example.course04

object ConstructorApp {
def main(args: Array[String]): Unit = {
val student = new Student("PK", 18, "Math")
println(student.name + ": " + student.major)

println(student) // 调用重写的toString方法
}
}

// 主构造器
class Person(val name: String, val age: Int) {

println("Person Constructor enter....")
val school = "ustc"
var gender: String = _

// 附属构造器
def this(name: String, age: Int, gender: String) {
this(name, age) // 附属构造器的第一行必须要调用主构造器或者其他附属构造器
this.gender = gender
}

println("Person Constructor leave....")
}

class Student(name: String, age: Int, var major: String) extends Person(name, age) {
println("Person Student enter....")

override val school: String = "pku" // 重写属性
override def toString: String = "override to String " + school // 重写方法

println("Person Student leave....")
}

抽象类

package org.example.course04

object AbstactApp {
def main(args: Array[String]): Unit = {
val stu = new Student2()
println(stu.name)
stu.speak
}
}

/*
抽象类:
类的一个或者多个方法没有完整的实现(只有定义,没有实现)
使用抽象类需要子类继承重写父类方法和属性
*/

abstract class Person2{
def speak // 只定义,没有实现

val name:String
val age:Int
}

class Student2 extends Person2{
override def speak: Unit = {
println("student2 speak")
}

override val name: String = "PK"
override val age: Int = 18
}

伴生类,伴生对象与apply

package org.example.course04

object ApplyApp {
def main(args: Array[String]): Unit = {
// for(i <- 1 to 10){
// ApplyTest.incr // object 不需要new
// }
// println(ApplyTest.count) // 10 说明object本身是单例对象
val b = ApplyTest() // 调用 Object.apply
println("!!!!!!!!!!")
var c = new ApplyTest() // 正常 new Class
println(c)
c() // 调用class apply方法


}
}

/*
apply
类名() ==> (调用) Object.apply
对象() ==> (调用) Class.apply
最佳实践:在Object的apply方法中去new class
*/


// 伴生类
class ApplyTest{
def apply()={
println("class ApplyTest apply...")
}
}

// 伴生对象
object ApplyTest{
println("Object ApplyTest enter...")
var count = 0;
def incr={
count += 1
}

def apply()={
println("Object ApplyTest apply...")

// 在object中的apply中new class
new ApplyTest
}

println("Object ApplyTest leave...")
}

集合

定长数组

package org.example.course05

object ArrayApp extends App {

val a = new Array[String](5); // 长度为5的数组

var b = Array("Hadoop","spart","storm")

var c = Array(2,3,3,4,5,6,7,8)
println(c.sum)
println(c.max)
println(c.min)
println(c.mkString("<",",",">")) //转为字符串,并添加分隔符
}

变长数组

package org.example.course05

object ArrayApp extends App {
var c = scala.collection.mutable.ArrayBuffer[Int]()
c += 1 // 添加元素
c += 2
c += (3,4,5)
c ++= Array(6,7,8) // 加数字用两个+号
c.insert(0,-1,-2)
c.remove(1,2) // 删除2个元素
for(ele <- c){
println(ele)
}
println(c.toArray.mkString) // 转为定长数组
}

List

package org.example.course05

object ListApp extends App {
val l = List(1,2,3,4,5)
println(l.head) //第一个元素
println(l.tail) // 除第一个元素以外的所有元素

val l2 = 1 :: Nil // 列表拼接,需要与列表类型
val l3 = 2 :: l2
val l4 = 1 :: 2::3 :: Nil
println(l3)
println(l4)

var l5 = scala.collection.mutable.ListBuffer[Int]()
l5 += 2
l5 ++=l4
println(l5)
println(l5.head)
println(l5.tail.head)

println(sum(l5:_*))
def sum(nums:Int*):Int={
if(nums.length==0){
0
}else{
nums.head + sum(nums.tail:_*) // _* 转为可变参数
}
}
}



以上是关于scala学习记录一的主要内容,如果未能解决你的问题,请参考以下文章

scala记录介绍

Scala学习记录——1.初识Scala语言

Scala的安装使用

Scala的安装使用

寒假学习记录2_Scala解释器的使用以及Scala程序的编译执行

寒假学习记录5_Scala面向对象编程基础1