加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – 我可以用虚拟课程做什么?

发布时间:2020-12-16 09:40:40 所属栏目:安全 来源:网络整理
导读:我已经看到(并听到)相当多的噪音关于添加虚拟课程到Scala(根据 Martin Odersky已经有 virtual types)。 什么是外行人的观点(可能是一个例子)什么是虚拟类型,什么可能是Scala有虚拟类? ([我没有C或C的经验,所以更喜欢任何答案不提及这些语言]) 解决方法 虚
我已经看到(并听到)相当多的噪音关于添加虚拟课程到Scala(根据 Martin Odersky已经有 virtual types)。

什么是外行人的观点(可能是一个例子)什么是虚拟类型,什么可能是Scala有虚拟类?

([我没有C或C的经验,所以更喜欢任何答案不提及这些语言])

解决方法

虚拟类型很简单:

>类和特征可以有类型成员。例如。

trait Foo {
  type T
}

>它们可以被精炼(但是一旦定义就不会被重写):

class Foo1 extends Foo {
  type T <: AnyVal
}

class Foo2 extends Foo1 {
  override type T = Boolean
}

class Foo3 extends Foo2 {
  // override type T = Int // rejected by the compiler – would be unsound
}

这是an example of virtual classes in a Java-descendent language(cclass是虚拟类):

Features of Virtual Classes

Let’s look
to another example to study the
possibilities of virtual classes. We
will use virtual classes to extend a
collaboration with a totally new
funtionality. Let’s say we have a core
data model to represent expressions:

06002

The collaboration defines Expr as the
base class for all expressions,
concrete classes to represent
constants,addition and
multiplication. Class BinaryExpr
implements the common functionality of
all expressions with two operands.
Note that the current version of
Caesar does not support constructors
with parameters and abstract methods
in cclass. The code below demonstrates
how sample expressions can be built
using such collaboration:

06003

The collaboration defines Expr as the
base class for all expressions,addition and
multiplication. Class BinaryExpr
implements the common functionality of
all expressions with two operands.

There are a lot of different
functionality related with
expressions: their evaluation,
formatting expressions to simple text
in infix or postfix order,various
consistency checks,lookups and
transformations. We want to keep all
this specific functionality separated
from each other and from the core data
model. This can be achieved with the
help of virtual classes. For example,
the collaboration below extends the
core model with simple expression
formatting functionality:

06004

This short example demonstrates
various features of virtual classes:

  • There is no need to repeat inheritance relationships between
    virtual classes if they are already
    defined in the supercollaboration. For
    example ExprModel defines Constant as
    subclass of Expr. It means that
    Constant is implicitly assumed as
    subclass of Expr in ExprFormat as
    well.

  • Virtual classes can use the fields and methods defined in their
    older versions. For example
    ExprFormat.BinaryExpr can use fields
    _left and _right defined in ExprModel.BinaryExpr.

  • The functionality defined in the overridden virtual classes can be
    accessed without type casts. For
    example,fields _left and _right of
    BinaryExpr were initially declared
    with type Expr of ExprModel,which
    does not have method format(),but in
    the context of ExprFormat the new
    version of Expr is assumed as the type
    of _left and _right. So format() can
    be called without any type casts.

  • The methods introduced in the overridden virtual classes can be
    again overridden in the new versions
    of subclasses. For example overridden
    Expr introduces method format(),which
    can be overridden in BinaryExpr. While
    Add and Mult do not override this
    method further,they inherit the
    format() of BinaryExpr.

Besides the demonstrated properties,
the overridden virtual classes can
also

  • introduce new data fields,
  • implement new interfaces,
  • introduce new inheritance relationships.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读