class Student(var name: String,var gender : String,var ID:String){ // override:覆盖,重写 override def equals(obj: Any): Boolean = { println("equals..............") // 如果 obj 不为空,并且 ID=ID if (obj != null && obj.getClass == this.getClass){ var s =obj.asInstanceOf[Student] // 比较 ID return s.ID == ID } else { return false } } }
object TestApply { def main(args: Array[String]): Unit = { var s1 =new Student("小花","女","7987989") var s2 =new Student("小花","女","7987989") var s3=s1 // 如何判断对象是否相等 println(s1==s2) // println(s2==s1) println(s1==1) // println(s1==s3)