2010-06-14 38 views
20

Tôi đã nhìn thấy (và nghe thấy) khá nhiều tiếng ồn về việc thêm lớp học ảo vào Scala (nó đã có virtual types, theo Martin Odersky).Tôi có thể làm gì với các lớp học ảo?

Quan điểm của giáo dân là gì (có lẽ với ví dụ) về loại ảo là gì và điều gì có thể có được là Scala có lớp học ảo không?

([Tôi không có kinh nghiệm với C hoặc C++, vì vậy muốn bất kỳ câu trả lời không đề cập đến các thứ tiếng].)

+0

Các Scala nhật thực plugin được sử dụng để được triển khai bằng các lớp ảo. Chúng ta đang nói về những điều tương tự? http://lampsvn.epfl.ch/trac/scala/wiki/VirtualClasses –

+0

Tôi không biết! Đó là lý do tại sao tôi hỏi câu hỏi này! –

Trả lời

18

loại ảo rất đơn giản:

  • Lớp học và đặc điểm có thể có các thành viên loại. Ví dụ.

    trait Foo { 
        type T 
    } 
    
  • Chúng có thể được tinh chế (nhưng không ghi đè một lần xác định):

    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 
    } 
    

Dưới đây là an example of virtual classes in a Java-descendent language (cclass là một lớp học ảo):

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:

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 {} 
} 

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; 
} 

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:

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

ví dụ đầu tiên của bạn không biên dịch: scala> class Foo2 mở rộng Foo1 { | loại ghi đè T = Boolean | } : 10: lỗi: ghi đè loại T trong lớp Foo1, bằng với Int; loại T có loại không tương thích loại ghi đè T = Boolean ^ –

+0

'loại ghi đè T = Boolean'? Điều đó sẽ không biên dịch. (Chỉ cần nhận thấy Oleg đã nói điều tương tự ở trên.) – missingfaktor

Các vấn đề liên quan