我希望通过为该接口(interface)中的属性提供集合访问器来“扩展”该接口(interface)。界面看起来像这样:interfaceIUser{stringUserName{get;}}我想要这样的东西:interfaceIMutableUser:IUser{stringUserName{get;set;}}我需要继承。我无法将IUser的主体复制到IMutableUser中并添加设置访问器。这在C#中可行吗?如果可以,如何实现? 最佳答案 我看不出有任何理由说明您发布的内容不起作用?刚刚做了一个快速测试,它编译正常,但给出了
我想要Dictionary的所有功能但我想要它作为Foo.我应该怎么做?目前我正在使用classFoo:Dictionary{/*I'mgettingallsortsoferrorsbecauseIdon'tknowhowtooverloadtheconstructorsoftheparentclass.*///overloadedmethodsandconstructorsgoeshere.Foo():base(){}Foo(intcapacity):base(capacity){}}重载父类的构造函数和方法的正确方法是什么?注意:我想我误用了“过载”这个词,请更正或建议更正。
我想要Dictionary的所有功能但我想要它作为Foo.我应该怎么做?目前我正在使用classFoo:Dictionary{/*I'mgettingallsortsoferrorsbecauseIdon'tknowhowtooverloadtheconstructorsoftheparentclass.*///overloadedmethodsandconstructorsgoeshere.Foo():base(){}Foo(intcapacity):base(capacity){}}重载父类的构造函数和方法的正确方法是什么?注意:我想我误用了“过载”这个词,请更正或建议更正。
我很高兴C#不允许您访问静态成员,“就好像”它们是实例成员一样。这避免了Java中的一个常见错误:Threadt=newThread(..);t.sleep(..);//Probablydoesn'tdowhattheprogrammerintended.另一方面,它确实允许您“通过”派生类型访问静态成员。除了运算符(它使您免于编写强制转换)之外,我想不出任何实际有用的情况。事实上,它会积极鼓励错误,例如://Nastysurprisesahead-won'tthrow;doessomethingunintended://CreatesaHttpWebRequestinstead.va
我很高兴C#不允许您访问静态成员,“就好像”它们是实例成员一样。这避免了Java中的一个常见错误:Threadt=newThread(..);t.sleep(..);//Probablydoesn'tdowhattheprogrammerintended.另一方面,它确实允许您“通过”派生类型访问静态成员。除了运算符(它使您免于编写强制转换)之外,我想不出任何实际有用的情况。事实上,它会积极鼓励错误,例如://Nastysurprisesahead-won'tthrow;doessomethingunintended://CreatesaHttpWebRequestinstead.va
我需要一个带有属性的基类,我可以在其中派生具有相同属性但不同(兼容)类型的类。基类可以是抽象的。publicclassBase{publicvirtualobjectprop{get;set;}}publicclassStrBase:Base{publicoverridestringprop{get;set;}//compilererror}publicclassUseIt{publicvoiduse(){Listl=newList();//...}}我尝试使用泛型,但在使用该类时出现问题,因为我想在列表中存储不同类型的基类。publicclassBaseG{publicTprop{g
我需要一个带有属性的基类,我可以在其中派生具有相同属性但不同(兼容)类型的类。基类可以是抽象的。publicclassBase{publicvirtualobjectprop{get;set;}}publicclassStrBase:Base{publicoverridestringprop{get;set;}//compilererror}publicclassUseIt{publicvoiduse(){Listl=newList();//...}}我尝试使用泛型,但在使用该类时出现问题,因为我想在列表中存储不同类型的基类。publicclassBaseG{publicTprop{g
我有一个具有虚拟属性的基类:publicvirtualstringName{get;set;}然后我有一个派生类,它只覆盖属性的getterpublicoverridestringName{get{return"FixedName";}}问题是这只会覆盖getter。如果有人调用setter,基类setter将被调用,而调用者不会知道它是无效的。所以我想我会做类似的事情:publicoverridestringName{get{return"FixedName";}set{thrownewException();}//whichexceptiontype?}所以两个(相关问题):有没有
我有一个具有虚拟属性的基类:publicvirtualstringName{get;set;}然后我有一个派生类,它只覆盖属性的getterpublicoverridestringName{get{return"FixedName";}}问题是这只会覆盖getter。如果有人调用setter,基类setter将被调用,而调用者不会知道它是无效的。所以我想我会做类似的事情:publicoverridestringName{get{return"FixedName";}set{thrownewException();}//whichexceptiontype?}所以两个(相关问题):有没有
当继承一个被继承的类时,新的/覆盖的行为不是我所期望的:$catProgram.csusingSystem;classA{publicvirtualvoidSayHi(){Console.WriteLine("FromA");}}classB:A{publicnewvirtualvoidSayHi(){Console.WriteLine("FromB");}}classC:B{publicoverridevoidSayHi(){Console.WriteLine("FromC");}}publicclassProgram{publicstaticvoidMain(){Ap=newC()