gpt4 book ai didi

interface - 定义一个继承接口(interface)但不实现接口(interface)的抽象类

转载 作者:行者123 更新时间:2023-12-04 11:31:26 25 4
gpt4 key购买 nike

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

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

有趣的是,我定义的类从上面定义了哪些 inerits 不显示公共(public) 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 但也提供自己的 Uri 的页面(用于 silverlight)(然后将它们粘贴在列表/数组中并将它们作为一组处理:
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 - 我想实现为属性 getter - 并且也遇到了问题。

这不编译
type IUriProvider = 
interface
abstract member uriString: String with get;
end

最佳答案

这是一种方法:

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

请注意,您必须提供接口(interface)的实现(因为 F# 中的所有接口(interface)实现都是显式的),但这只能引用类中的抽象成员。然后你可以这样子类化:
type ConcreteUriUserControl() =
inherit UriUserControl()
override this.Uri = null
override this.UriString = "foo"

关于interface - 定义一个继承接口(interface)但不实现接口(interface)的抽象类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2889548/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com