定义一个继承接口但不实现它的抽象类

发布于 2024-09-02 18:02:49 字数 1233 浏览 8 评论 0原文

合并它编译的 leppies 反馈 - 但在我看来有一些缺点,我希望编译器强制每个子类定义自己的 Uri 属性。现在的代码:

[<AbstractClass>] 
type UriUserControl() = 
    inherit UserControl()
    interface IUriProvider with 
        member this.Uri with get() = null

有趣的是,我从上面定义的 inerits 类不显示公共 Uri 属性:

type Page2() as this =
    inherit UriUserControl()
    let uriStr = "/FSSilverlightApp;component/Page2.xaml"
    let mutable uri = new System.Uri(uriStr, System.UriKind.Relative)
    do
        Application.LoadComponent(this, uri)

    member public this.Uri with get () = uri

我想定义一个继承自 UserControl 和我自己的 Interface IUriProvider 的抽象类,但不实现它。目标是能够定义实现 UserControl 的页面(对于 silverlight),但也提供自己的 Uri(然后将它们粘贴在列表/数组中并将它们作为一组处理:

type IUriProvider = 
interface
    abstract member uriString: String ;
    abstract member Uri : unit -> System.Uri ;
end

[<AbstractClass>] 
type UriUserControl() as this = 
    inherit IUriProvider with
        abstract member uriString: String ;
    inherit UserControl()

还有定义中的 Uri - 我希望作为属性获取器来实现 - 我也遇到了问题,

这也无法编译。

type IUriProvider = 
    interface
        abstract member uriString: String with get;
end

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:

[<AbstractClass>] 
type UriUserControl() = 
    inherit UserControl()
    interface IUriProvider with 
        member this.Uri with get() = null

Interesting enough, the class I defines which inerits from above does not show a public Uri property:

type Page2() as this =
    inherit UriUserControl()
    let uriStr = "/FSSilverlightApp;component/Page2.xaml"
    let mutable uri = new System.Uri(uriStr, System.UriKind.Relative)
    do
        Application.LoadComponent(this, uri)

    member public this.Uri with get () = uri

I would like to define an abstract class that inherits from UserControl and my own Interface IUriProvider, but doesn't implement it. The goal is to be able to define pages (for silverlight) that implement UserControl but also provide their own Uri's (and then stick them in a list / array and deal with them as a set:

type IUriProvider = 
interface
    abstract member uriString: String ;
    abstract member Uri : unit -> System.Uri ;
end

[<AbstractClass>] 
type UriUserControl() as this = 
    inherit IUriProvider with
        abstract member uriString: String ;
    inherit UserControl()

Also the Uri in the definition - I would like to implement as a property getter - and am having issues with that as well.

this does not compile

type IUriProvider = 
    interface
        abstract member uriString: String with get;
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

∞觅青森が 2024-09-09 18:02:49

有一种方法可以做到这一点:

type IUriProvider =  
    abstract member UriString: string
    abstract member Uri : System.Uri

[<AbstractClass>]  
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

请注意,您必须提供接口的实现(因为 F# 中的所有接口实现都是显式的),但这只能引用类中的抽象成员。然后你可以这样子类化:

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

Here is a way to do it:

type IUriProvider =  
    abstract member UriString: string
    abstract member Uri : System.Uri

[<AbstractClass>]  
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"
凤舞天涯 2024-09-09 18:02:49

从 .NET 的角度来看,您至少需要为该接口提供一个抽象实现。但是,由于默认的接口可访问性,这再次可能会出现问题,这将需要更多的粘合才能显式实现。

From a .NET point of view, you would need to at least provide an abstract implementation for the interface. But that again could proof problematic due to default interface accessibility, which would require some more glue again for an explicit implementation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文