开发者

Should I use inherit or interface?

What is the difference between:

type IFooable =
    interface IDisposa开发者_如何学Goble

    abstract Foo : (unit -> unit)

and

type IFooable =
    inherit IDisposable

    abstract Foo : (unit -> unit)

?

If equivalent, in which cases should I use one over the other ? Thanks!


(I originally thought that) You must use 'inherit' with (at most one) base class. You can use 'interface' for any interfaces you are going to implement.

So in the case of IDisposable, it must be 'interface'.

EDIT, ok the compiler allows it, but that might be a bug, I'll look

EDIT: turns out it's a likely bug the other way, and likely interfaces will force you to use 'inherit' to inherit other interfaces, the idea being that 'inherited' members are always directly visible in the 'implicit interface' sense, whereas an 'interface' declaration on a class is an 'explicit' interface that requires a cast to that interface type to use those members. One way or another, we're likely to remove this flexibility in the language syntax so that there is only one way to author this, rather than two equivalent ways.


I tend to use inheritance for true oop (i.e. X isa Y) and interfaces as a sort of decorator implementation.

probably not the defacto "proper" way to do it, but we quite like it.

what I mean is X will inherit Y, but then we'll want to give it some extra functionality from V, W, and Z.

"Jetta isa Car, but is also IDiesel, IGerman, IBroken"

this is just the way my current project does it, and I'll probably get downvoted for "abuse of oop", or something :)


u should use interfaces if you are only going to use abstract methods. Because you can only inherit one class but you can implement many interfaces.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜