Scala中的case class
case class是Scala中一种特殊的类,它用于创建不可变的数据容器
//Set特点:去重
//设置一个Book。三个属性:书名,作者,价格
class Book1(bookName:String,author:String,price:Double){
//判断一个类的两个对象是否相等
// override def equals(obj: Any): Boolean = {
// //判断依据是书名相同
// val o=obj.asInstanceOf[ttttt.Book]
// o.bookName==bookName
// }
//
// override def toString: String = s"书名是:$bookName,作者是:$author,价格是:$price"
//
// override def hashCode(): Int = bookName.hashCode
}
//casa class的特点
//1.数据是只读的
//2.可以省略new
//3.它内置实现了一些方法
// equals hashCode toString
case class BookCase(bookName:String,author:String,price:Double)
object t4 {
def main(args: Array[String]): Unit = {
val book1=new Book1("西游记","吴承恩",100)
val book11=new Book1("西游记","吴承恩",100)
val set1=Set(book1,book11)
println(set1)
val book2=BookCase("西游记","吴承恩",100)
val book3=BookCase("西游记","吴承恩",100)
println(book1 == book11)
println(book2 == book3)
println(book1)//println会自动去调用对象的toString方法
println(book2)
}
}