网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

scala的类型上下界是什么

本篇内容主要讲解“scala的类型上下界是什么”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“scala的类型上下界是什么”吧!

创新互联-专业网站定制、快速模板网站建设、高性价比莒县网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式莒县网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖莒县地区。费用合理售后完善,十载实体公司更值得信赖。

类型上界

在Scala中,类型参数和抽象类型都可以有一个类型边界约束。这种类型边界在限制类型变量实际取值的同时还能展露类型成员的更多信息。比如像T <: A这样声明的类型上界表示类型变量T应该是类型A的子类。下面的例子展示了类PetContainer的一个类型参数的类型上界。

abstract class Animal { def name: String}
abstract class Pet extends Animal {}
class Cat extends Pet {  override def name: String = "Cat"}
class Dog extends Pet {  override def name: String = "Dog"}
class Lion extends Animal {  override def name: String = "Lion"}
class PetContainer[P <: Pet](p: P) {  def pet: P = p}
val dogContainer = new PetContainer[Dog](new Dog)val catContainer = new PetContainer[Cat](new Cat)

// this would not compileval lionContainer = new PetContainer[Lion](new Lion)

类PetContainer接受一个必须是Pet子类的类型参数P。因为Dog和Cat都是Pet的子类,所以可以构造PetContainer[Dog]和PetContainer[Cat]。但在尝试构造PetContainer[Lion]的时候会得到下面的错误信息:

type arguments [Lion] do not conform to class PetContainer's type parameter bounds [P <: Pet]

这是因为Lion并不是Pet的子类。

类型下界

类型上界 将类型限制为另一种类型的子类型,而 类型下界 将类型声明为另一种类型的超类型。术语 B >: A 表示类型参数 B 或抽象类型 B 是类型 A 的超类型。在大多数情况下,A 将是类的类型参数,而 B 将是方法的类型参数。
下面看一个适合用类型下界的例子:
   
trait Node[+B] {                def prepend(elem: B): Node[B]              }              case class ListNode[+B](h: B, t: Node[B]) extends Node[B] {                def prepend(elem: B): ListNode[B] = ListNode(elem, this)                def head: B = h                def tail: Node[B] = t              }              case class Nil[+B]() extends Node[B] {                def prepend(elem: B): ListNode[B] = ListNode(elem, this)              }  
该程序实现了一个单链表。Nil 表示空元素(即空列表)。class ListNode 是一个节点,它包含一个类型为 B (head) 的元素和一个对列表其余部分的引用 (tail)。class Node 及其子类型是协变的,因为我们定义了 +B。
但是,这个程序 不能 编译,因为方法 prepend 中的参数 elem 是协变的 B 类型。这会出错,因为函数的参数类型是逆变的,而返回类型是协变的。
要解决这个问题,我们需要将方法 prepend 的参数 elem 的型变翻转。我们通过引入一个新的类型参数 U 来实现这一点,该参数具有 B 作为类型下界。
   
trait Node[+B] {                def prepend[U >: B](elem: U): Node[U]              }              case class ListNode[+B](h: B, t: Node[B]) extends Node[B] {                def prepend[U >: B](elem: U): ListNode[U] = ListNode(elem, this)                def head: B = h                def tail: Node[B] = t              }                  case class Nil[+B]() extends Node[B] {                def prepend[U >: B](elem: U): ListNode[U] = ListNode(elem, this)              }
现在我们像下面这么做:  
   trait Bird
      
    
    case class AfricanSwallow() extends Bird
      
    
    case class EuropeanSwallow() extends Bird
   
    val africanSwallowList= ListNode[AfricanSwallow](AfricanSwallow(), Nil())
      
    
    val birdList: Node[Bird] = africanSwallowList
      
    
    birdList.prepend(EuropeanSwallow())
可以为 Node[Bird] 赋值 africanSwallowList,然后再加入一个 EuropeanSwallow。

到此,相信大家对“scala的类型上下界是什么”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!


当前题目:scala的类型上下界是什么
网址分享:http://bjjierui.cn/article/pjeses.html

其他资讯