我有这样的类层次结构。
abstract class Element {
var name: String
var description: String
}
class Group (var name : String, var description : String, var members: Set[Element] =Set())
extends Element{}
trait TypedElement extends Element{
var types: Set[Type]
}
trait ArchitecturalElement extends PropertyHolderElement with TypedElement
abstract class Component extends ConnectableElement with ArchitecturalElement {
var subElements : Set[ArchitecturalElement]
var interactionPoints : Set[InteractionPoint]
}
class SAComponent ( var name :String,
var description : String,
var types : Set[Type] = Set(),
var subElements : Set[ArchitecturalElement] = Set(),
var interactionPoints : Set[InteractionPoint] = Set()
) extends Component
Element 是我模型的根类。现在我想在 Element 上放置一个抽象方法“验证”,以便我的所有类继承。并且只在具体实施。我该怎么做呢?我经过验证的方法必须具有不同的签名,并且有些方法返回一个值,但其他方法不返回任何内容。
例如,我想要这样的东西:
abstract class Element {
var name: String
var description: String
def validate (elem: Element)
}
class Group (var name : String, var description : String, var members: Set[Element] =Set())
extends Element{
validate (gr: Group) = {//the method body}
}
trait TypedElement extends Element{
var types: Set[Type]
validate (ty : TypedElement )
}
trait ArchitecturalElement extends PropertyHolderElement with TypedElement {
validate (arel: ArchitecturalElement )
}
abstract class Component extends ConnectableElement with ArchitecturalElement {
var subElements : Set[ArchitecturalElement]
var interactionPoints : Set[InteractionPoint]
validate (el : Component, subElem: Set[ArchitecturalElement]) : Boolean = {//the method body}
//here, I need the return value to avoid cycles of recursion
}