C#支持返回類型協方差嗎?我正在使用.NET框架,我非常希望能夠制作一個自定義類型的頁面,我的所有網站都使用它。當我試圖從控件訪問頁面時,問題就出現了。我希望能夠返回特定類型的頁面,而不是默認頁面。有什么辦法嗎?public class MyPage : Page{
// My own logic}public class MyControl : Control{
public MyPage Page { get; set; }}
3 回答

慕的地10843
TA貢獻1785條經驗 獲得超8個贊
abstract class Enclosure{ public abstract Animal Contents();}class Aquarium : Enclosure{ public override Fish Contents() { ... }}
abstract class Enclosure{ protected abstract Animal GetContents(); public Animal Contents() { return this.GetContents(); }}class Aquarium : Enclosure{ protected override Animal GetContents() { return this.Contents(); } public new Fish Contents() { ... }}

瀟瀟雨雨
TA貢獻1833條經驗 獲得超4個贊
public new MyPage Page {get return (MyPage)Page; set;}'
Page
MyPage
Control
MyPage
Page
Page
Control
- 3 回答
- 0 關注
- 464 瀏覽
添加回答
舉報
0/150
提交
取消