前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >[4] - 类和对象之基础

[4] - 类和对象之基础

作者头像
codingforfun
发布2018-08-24 15:18:15
3590
发布2018-08-24 15:18:15
举报

这篇文章将介绍类的基础知识

定义

Scala 中以 class 来作为类的声明,在类中可以定义成员和方法,成员和方法可以有不同的可见性(这个会在后文详述)。

代码语言:javascript
复制
scala> class Company {
     |   private var employeeCount = 0
     |   def getEmployeeCount(): Int = employeeCount
     |   def setEmployeeCount( count: Int)= {
     |     employeeCount = count
     |   }
     |
     |   def m( i: Int ) {}
     |   def m( str: String ) {}
     | }
defined class Company

构造器

Scala 中,类有一个主构造器,主构造器必须包含所需的所有参数。除了一个主构造器,还可以有0个或多个辅助构造器,辅助构造器又称次构造器。辅助构造器命名为 this,其第一条语句必须调用主构造器或其他辅助构造器,来看下面的例子:

代码语言:javascript
复制
scala> class T ( x1: Int, y1: String, z1: Double ) {
     |   private val xx1 = x1
     |   private val yy1 = y1
     |   private val zz1 = z1
     |
     |   def this ( x1: Int, y1: String ) {
     |     this( x1, y1, 1.0 )
     |   }
     |
     |   def this ( x1: Int ) {
     |     this( x1, "" )
     |   }
     | }
defined class T

还有一点需要注意的是,被调用的辅助构造函数的定义必须放在主动调用的辅助构造函数前面,不然会报错:

代码语言:javascript
复制
scala> class T ( x1: Int, y1: String, z1: Double ) {
     |   private val xx1 = x1
     |   private val yy1 = y1
     |   private val zz1 = z1
     |
     |   def this ( x1: Int ) {
     |     this( x1, "" )
     |   }
     |
     |   def this ( x1: Int, y1: String ) {
     |     this( x1, y1, 1.0 )
     |   }
     | }
<console>:13: error: called constructor's definition must precede calling constructor's definition
           this( x1, "" )
           ^

不管辅助函数调来调去,最终都还是要调用到主构造函数,这确保了新实例的初始化逻辑一致。

如果在主构造函数的参数前加 var 或 val,该参数就成为实例的一个成员,这部分知识在Scala case class那些你不知道的知识有更详细的介绍

重载

Scala 类方法允许重载,如类 Company 中的 m 方法。重载要求参数列表和返回类型不完全相同,但参数名可相同,这是因为编译后是通过方法名、参数列表、返回类型综合来区分各个方法的。

在方法重载时,有一点需要注意:对于『高级类型』,存在类型擦除机制,所谓的高级类型就是包含类型参数的类型,比如 ListA,下面这个例子可以展示了类型擦除:

代码语言:javascript
复制
scala> class Tmp {
     |   def m( data: List[Int] ) {}
     |   def m( data: List[String] ) {}
     | }
<console>:9: error: double definition:
method m:(data: List[String])Unit and
method m:(data: List[Int])Unit at line 8
have same type after erasure: (data: List)Unit
         def m( data: List[String] ) {}
             ^

报了有相同类型的参数的错误。

类型成员

Scala 允许你在类内部定义类型成员,在构造类实例的时候指定该类型成员对应的具体类型。类型成员可用于类内部的成员或函数,提供了更好的泛华能力,从下面这个简单的例子可以看出:

代码语言:javascript
复制
scala> class T {
     |   type X
     |
     |   def getClassName( x: X): String = {
     |     x.getClass.getTypeName
     |   }
     | }
defined class T

scala> val x1 = new T{ type X = Int }
x1: T{type X = Int} = $anon$1@515f550a

scala> x1.getClassName(10)
res0: String = java.lang.Integer

scala> val x2 = new T{ type X = String }
x2: T{type X = String} = $anon$1@61a52fbd

scala> x2.getClassName("string")
res1: String = java.lang.String

当然,也可以在类外部定义类型变量,如:

代码语言:javascript
复制
scala> type L = List[Int]
defined type alias L

方法与成员同名

与 JAVA 不同,如果方法参数列表不为空,该方法可以与成员同名,如:

代码语言:javascript
复制
scala> class T {
     |   private val m = 0
     |
     |   def m( i: Int ): Int = m + i
     | }
defined class T

传送门Scala 在简书目录


本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016.05.25 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 定义
  • 构造器
  • 重载
  • 类型成员
  • 方法与成员同名
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档