我可以用虚拟课程做什么? [英] What can I do with virtual classes?

查看:38
本文介绍了我可以用虚拟课程做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到(并听到)很多关于将虚拟类添加到 Scala(它已经有虚拟类型,根据 马丁奥德斯基).

I've seen (and heard) quite a bit of noise about adding virtual classes to Scala (it already has virtual types, according to Martin Odersky).

对于什么是虚拟类型以及什么是可能的,Scala 拥有虚拟类是什么?

What is a layman's perspective (perhaps with an example) on what a virtual type is and what could be possible were Scala to have virtual classes?

([我没有使用 C 或 C++ 的经验,所以希望任何答案都不要提及这些语言].)

推荐答案

虚拟类型很简单:

  • 类和特征可以有类型成员.例如

  • Classes and traits can have type members. E.g.

trait Foo {
  type T
}

  • 它们可以被细化(但一旦定义就不能被覆盖):

  • They can be refined (but not overridden once defined):

    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
    }
    

  • 这是Java 后裔语言中的虚拟类示例(cclass 是一个虚拟类):

    Here is an example of virtual classes in a Java-descendent language (cclass is a virtual class):

    虚拟课堂的特点

    我们来看看另一个例子来研究虚拟课程的可能性.我们将使用虚拟类来扩展与全新的合作功能性.假设我们有一个核心表示表达式的数据模型:

    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:

    public cclass ExprModel {
       abstract public cclass Expr {}
    
       public cclass Constant extends Expr {
          protected int _val;
          public Constant(int val) { 
             _val = val;
          }                 
       }
    
       abstract public cclass BinaryExpr {
          protected Expr _left;
          protected Expr _right;
          public BinaryExpr(Expr left, Expr right) {
             _left = left;
             _right = right;
          }
       }
    
       public cclass Add extends BinaryExpr {}
       public cclass Mult extends BinaryExpr {} 
    }
    

    合作将 Expr 定义为所有表达式的基类,代表的具体类常数、加法和乘法.类 BinaryExpr实现了通用的功能带有两个操作数的所有表达式.请注意,当前版本Caesar 不支持构造函数带参数和抽象方法在c类中.下面的代码演示如何构建示例表达式使用这种协作:

    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:

    public model.Expr buildSampleExpr(final ExprModel model) {
       model.Expr const1 = model.new Constant(-3);
       model.Expr const2 = model.new Constant(2);
       model.Expr op1 = model.new Mult(const1, const2);
       model.Expr const3 = model.new Constant(5);
       model.Expr op2 = model.new Add(op1, const3);
       return op2;
    }
    

    合作将 Expr 定义为所有表达式的基类,代表的具体类常数、加法和乘法.类 BinaryExpr实现了通用的功能带有两个操作数的所有表达式.

    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.

    有很多不同的相关的功能表达式:他们的评估,将表达式格式化为简单文本以中缀或后缀顺序,各种一致性检查、查找和转换.我们想保留所有这个特定的功能分开了从彼此和核心数据模型.这可以通过虚拟课程的帮助.例如,下面的合作扩展了简单表达的核心模型格式化功能:

    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:

    public cclass ExprFormat extends ExprModel {
       abstract public cclass Expr {       
          abstract public void String format();
       }
    
       public cclass Constant {
          public void String format() { 
             return _val < 0 ? "(" + _val + ")" : "" + _val; 
          }
       }
    
       abstract public cclass BinaryExpr {
          public void String format() { 
             return "(" + _left.format() + getOperSymbol() 
                        + _right.format() + ")"; 
          }
          abstract public void String getOperSymbol();
       }
    
       public cclass Add {
          public void String getOperSymbol() { return "+"; }
       }
    
       public cclass Mult {
          public void String getOperSymbol() { return "*"; }
       }
    }
    

    这个简短的例子演示了虚拟类的各种特性:

    This short example demonstrates various features of virtual classes:

    • 之间无需重复继承关系虚拟课程,如果他们已经在超级协作中定义.为了示例 ExprModel 将 Constant 定义为Expr 的子类这意味着常数被隐式假定为ExprFormat 中 Expr 的子类为好吧.

    • 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.

    虚拟类可以使用它们定义的字段和方法旧版本.例如ExprFormat.BinaryExpr 可以使用字段_left 和 _right 在 ExprModel.BinaryExpr 中定义.

    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.

    在重写的虚拟类中定义的功能可以是无需类型转换即可访问.为了例如,字段 _left 和 _rightBinaryExpr 最初被声明类型为 ExprModel 的 Expr,其中没有方法 format(),但在ExprFormat 的上下文新Expr 的版本被假定为类型_left 和 _right.所以 format() 可以无需任何类型转换即可调用.

    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.

    被覆盖的虚类中引入的方法可以是在新版本中再次被覆盖的子类.例如覆盖Expr 引入了方法 format(),其中可以在 BinaryExpr 中被覆盖.尽管Add 和 Mult 不会覆盖这个方法进一步,他们继承了BinaryExpr 的 format().

    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

    • 引入新的数据字段,
    • 实现新接口,
    • 引入新的继承关系.

    这篇关于我可以用虚拟课程做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆