定义一个继承接口但不实现它的抽象类
合并它编译的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有一种方法可以做到这一点:
请注意,您必须提供接口的实现(因为 F# 中的所有接口实现都是显式的),但这只能引用类中的抽象成员。然后你可以这样子类化:
Here is a way to do it:
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:
从 .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.