Define an abstract class that inherits an infterface, but does not implement it

前端 未结 2 379
执念已碎
执念已碎 2021-01-12 12:29

incorporating leppies feedback it compiles - but IMO some drawbacks I want each sub class to be forced by the compiler to define their own Uri property. Code as it is now:

2条回答
  •  心在旅途
    2021-01-12 13:09

    Here is a way to do it:

    type IUriProvider =  
        abstract member UriString: string
        abstract member Uri : System.Uri
    
    []  
    type UriUserControl() as this =  
        inherit System.Windows.Controls.UserControl() 
        abstract member Uri : System.Uri
        abstract member UriString : string
        interface IUriProvider with 
            member x.Uri = this.Uri
            member x.UriString = this.UriString
    

    Note that you have to provide an implementation of the interface (since all interface implementations in F# are explicit), but this can just refer back to abstract members in the class. Then you can subclass thusly:

    type ConcreteUriUserControl() =
        inherit UriUserControl()
        override this.Uri = null
        override this.UriString = "foo"
    

提交回复
热议问题