Scala 类型层级:

对象相等性:

和Java一样要重写equals方法和hashcode方法

class Student(val id: Int, val name: String) {
override def equals(other: Any) = {
val that = other.asInstanceOf[Student]
if (that == null) false
else id == that.id && name == that.name
} override def hashCode = 13 * id.hashCode() + 17 * name.hashCode() } object TestCase {
def main(args: Array[String]): Unit = { val s1 = new Student(1,"Sky")
val s2 = new Student(1,"Sky") println(s1.equals(s2))
println(s1 == (s2)) } }

返回结果:

true

true

快学Scala 第十三课 (类型层级,对象相等性)的更多相关文章

  1. 快学Scala 第十一课 (类继承)

    类继承: class People { } class Emp extends People{ } 和Java一样,final的类不能被继承.final的字段和方法不能被override. 在Scal ...

  2. 快学Scala 第八课 (嵌套类)

    嵌套类: class Human { class Student{ val age = 10 } } object ClassDemo { def main(args: Array[String]): ...

  3. 快学Scala 第十课 (包和包对象)

    Scala包定义: 嵌套式: package a1 { class a1Class{ val age = 10 } package a2 { class PackageTest { def main( ...

  4. 快学Scala 第五课 (构造映射,获取映射值,更新映射值,迭代映射,与Java互操作)

    构造映射: val score = Map[String, Int]() val score1 = HashMap[String, Int]() val value1 = Map[String, In ...

  5. 快学Scala 第四课 (多维数组,与Java集合的互操作)

    Scala二维数组的定义: val arr2 = Array.ofDim[String](2, 2) arr2(0)(0) = "aa" arr2(1)(0) = "bb ...

  6. 快学Scala 第三课 (定长数组,变长数组, 数组循环, 数组转换, 数组常用操作)

    定长数组定义: val ar = new Array[Int](10) val arr = Array("aa", "bb") 定长数组赋值: arr(0) = ...

  7. 快学Scala 第七课 (类构造函数)

    类 主构造器: class Person (var name: String){ } 主构造参数可以不带val或者var,如果没有被其他方法使用,则不保存为字段. 如果被其他方法使用,则被升格为字段, ...

  8. 快学Scala 第六课 (类getter和setter)

    类getter和setter 如果字段定义是private[this], 字段是私有的,但不生成getter和setter方法. class Counter { private[this] var v ...

  9. 快学Scala 第二十一课 (初始化trait的抽象字段)

    初始化trait的抽象字段: trait Logged { println("Logged constructor") def log(msg: String){ println( ...

随机推荐

  1. zstu19一月月赛 duxing201606的原味鸡树

    duxing201606的原味鸡树 题意: 给定一颗有n(n<=1e9)个节点的完全二叉树,1e5次询问,问某个节点有几个子节点. 思路: 自己在月赛上没有思路,问了zfq才知道. 设两个指标, ...

  2. 区间dp专题

    HDU4283You Are the One区间dp, 记忆话搜索运行时间:   #include <iostream> #include <cstdio> #include ...

  3. SDU暑期集训排位(2)

    A. Art solved by sdcgvhgj 3min 签到 B. Biology solved by sdcgvhgj 85min 暴力 C - Computer Science solved ...

  4. CF992B Nastya Studies Informatics 数学(因子) 暴力求解 第三道

    Nastya Studies Informatics time limit per test 1 second memory limit per test 256 megabytes input st ...

  5. Spring 两大核心 IOC 和 AOP

    如果你的简历上写着Spring (请详述一下spring的两大核心)这个问题一定会被问到. 一.什么叫IOC 1. IOC 全称(Inversion of Control)-- 控制反转. IOC 只 ...

  6. 湘潭大学oj循环1-5

    #include <stdio.h>#include <stdlib.h> int main(){   int b,s,n;    int a[101]; A:scanf(&q ...

  7. React Hooks 你不来了解下?

    前言 最近在看 React 的新语法-- React Hooks,只能一句话概括:React 语法真的是越来越强大,越写代码越少. 强烈推荐还没看 React Hooks 的同学去学习下,这会让你写r ...

  8. 【第十五篇】easyui datagrid的列编辑,同时插入两张表的数据进去

    看图说话. 需求:插入两张表,上面的表单是第一张表的内容,下面的两个表格是第二张详情表的内容,跟第一张表的id关联 第二张表有一个列是需要用户手动填写添加的. 国际惯例,上代码 <div id= ...

  9. response向客户端写入数据

    1.写入文字: protected void doGet(HttpServletRequest request, HttpServletResponse response) throws Servle ...

  10. TestNG(三) 基本注解BeforeMethod和AfterMethod

    package com.course.testng; import org.testng.annotations.*; public class BasicAnnotation { @Test //最 ...