package test_27
//Set的特点:唯一(元素不同);无序
//case class定义一组数据
case class Book(var bookName:String,var author:String,var price:Double){}
object caseclass {def main(args: Array[String]): Unit = {//定义一个可变setval set1=scala.collection.mutable.Set[Book]()//向Set中添加两条一样的数据val book1=new Book("西游记","吴承恩",100)val book2=new Book("西游记","吴承恩",100)val book3=new Book("西游记","吴承恩",100)println(book1==book2)set1+=book1set1+=book2set1+=book3set1.foreach(s=>{println(s.author)})}}
去掉case还能否去重?
package test_27
//Set的特点:唯一(元素不同);无序
//case class定义一组数据
//case class Book(var bookName:String,var author:String,var price:Double){
//
//}class Book(var bookName:String,var author:String,var price:Double){//判断是否相等的逻辑bookName相等override def equals(obj: Any): Boolean = {
// println("equal")
// trueval ob=obj.asInstanceOf[Book]ob.bookName==bookName}override def hashCode(): Int = bookName.hashCodeoverride def toString: String = s"书名:$bookName\t 作者:$author \t 价格:$price"
}//1.属性的默认修饰是val(不可变),说明定义对象不能改属性
//2.可以省略new
//3.case class它自动提供:
//equals方法
//hashCode方法
//toString方法
case class Student( name:String)
object caseclass {def main(args: Array[String]): Unit = {val s1=new Student("小花")
println(s1)//定义一个可变setval set1=scala.collection.mutable.Set[Book]()//向Set中添加两条一样的数据val book1=new Book("西游记","吴承恩",100)val book2=new Book("西游记","吴承恩",100)val book3=new Book("西游记","吴承恩",100)println(book1==book2)set1+=book1set1+=book2set1+=book3set1.foreach(s=>{println(s.author)})}}