Scala中的伴生对象和 case class 样例类

case class

当一个类被定义成为case类后,Scala会自动帮你创建一个伴生对象并帮你实现了apply, unapply,setter, getter 和toString,equals,copy和hashCode等方法

什么是apply方法和unapply方法:

首先定义一个类

class MyStudent(val name: String, val age: Int){
  def getName(){
    println("name is : " + name + "age : " + age )
  }
}

实现 apply 方法

object MyStudent{  // 伴生对象 MyStudent,里面定义了apply方法
  // apply方法有点类似于java中的构造函数,接受构造参数变成一个对象。
  def apply(name: String, age: Int): MyStudent = {
    new MyStudent(name, age)
  }
}

调用方式,就可以不用写new 了

    val m1 = MyStudent("zhangsan", 26) //  由于定义了伴生对象, 可以不用使用new  进行创建对象
    m1.getName()

实现unapply方法:接受一个对象,从对象中提取出相应的值,用来做模式匹配

object MyStudent{  // 伴生对象 MyStudent,里面定义了apply方法
  // apply方法有点类似于java中的构造函数,接受构造参数变成一个对象。
  def apply(name: String, age: Int): MyStudent = {
    new MyStudent(name, age)
  }
  
  // unapply方法就刚好相反,他是接受一个对象,从对象中提取出相应的值。  用来最为模式匹配
  def unapply(myStudent: MyStudent): Option[(String, Int)] = {
      if (myStudent == null){
        None
      }else{
        Some(myStudent.name, myStudent.age)
      }
    }
}

这时:我们就可以使用模式匹配了:

    val m1 = MyStudent("zhangsan", 26) //  由于定义了伴生对象, 可以不用使用new  进行创建对象
    m1.getName()
    
    // 结合 unapply方法
    m1 match{
      case MyStudent("zhangsan_2", _) => println("zhangsan_2")
      case _ => println("not such student")
    }

所以case class 直接可以使用 val object = ClassDemo() 不用使用new 和直接使用 类的模式匹配了。

getter 方法和 hashCode方法, toString方法

scala> case class Student(name: String, age: Int, studentNum: Int)
defined class Student

scala> Student("zhangsan", 24, 1024)
res7: Student = Student(zhangsan,24,1024)

scala> val s = Student("zhangsan", 24, 1024)
s: Student = Student(zhangsan,24,1024)

scala> s.age.toString
res8: String = 24

scala> s.hashCode
res9: Int = -1184807636

scala> s.name
res10: String = zhangsan

scala> s.age
res11: Int = 24

scala> s.studentNum
res12: Int = 1024

 copy() 方法, equals方法

scala> val s1 = s.copy()
s1: Student = Student(zhangsan,24,1024)

scala> s1
res13: Student = Student(zhangsan,24,1024)

scala> s==s1
res14: Boolean = true

scala> s equals s1
res15: Boolean = true

setter方法

由于下面两行代码是对应的,默认是使用 val , 当我们要使用setter 方法时,需要使用var 可变的参数

case class Student(name: String, age: Int, studentNum): Int
case class Student(val name: String, val age: Int, val studentNum): Int

使用setter方法的方式

case class Student(var name: String, var age: Int, var studentNum): Int
// 这时, 我们就可以重新对name 等进行赋值
name = "zhaosi"

其他: 关于Scala中的 break 和 continue 的实现

Scala 中是没有break 和 continue 的, break可以使用 break(), breakable() , continue使用循环守卫,即使用判断语句

breakable 需要先导入包为: import util.control.Breaks._

break 的实现

import util.control.Breaks._    
breakable{
    for (i <- 1 until 100){
      println("this is i : " + i)
      if (i == 10) break()
    }
    }

输出为:

this is i : 1
this is i : 2
this is i : 3
this is i : 4
this is i : 5
this is i : 6
this is i : 7
this is i : 8
this is i : 9
this is i : 10

continue 的实现,结合判断语句实现:

    // continue 的实现
    println("cotinue=========")
    for (i <- 1 until 100){
      if (i < 20 && i !=15){     // 直接加个判断语句,Scala称为 循环守卫
        println("this is i: " + i)
      }
    }

输出为:

cotinue=========
this is i: 1
this is i: 2
this is i: 3
this is i: 4
this is i: 5
this is i: 6
this is i: 7
this is i: 8
this is i: 9
this is i: 10
this is i: 11
this is i: 12
this is i: 13
this is i: 14
this is i: 16
this is i: 17
this is i: 18
this is i: 19

项目推荐:

2000多G的计算机各行业电子资源分享(持续更新)

2020年微信小程序全栈项目之喵喵交友【附课件和源码】

Spring Boot开发小而美的个人博客【附课件和源码】

Java微服务实战296集大型视频-谷粒商城【附代码和课件】

Java开发微服务畅购商城实战【全357集大项目】-附代码和课件

最全最详细数据结构与算法视频-【附课件和源码】

在这里插入图片描述

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值