Tag: inheritance

为什么base()构造函数不是必需的?

我有类结构 abstract class Animal { public Animal(){ //init stuff.. } } class Cat : Animal { public Cat(bool is_keyboard) : base() //NOTE here { //other init stuff } } 现在,看看注意到的那一行。 如果你删除: base()然后它将编译没有错误。 为什么是这样? 有没有办法禁用此行为?

如何防止某些方法的inheritance?

如何防止派生类中某些方法或属性的inheritance?! public class BaseClass : Collection { //Some operations… //Should not let derived classes inherit ‘Add’ method. } public class DerivedClass : BaseClass { public void DoSomething(int Item) { this.Add(Item); // Error: No such method should exist… } }

为什么带有generics类型的基础Windows窗体表格类会停止设计器加载?

我试图有一个基本的Windows窗体表单,其中包含常见的function和控件 – 但也包含对一个类的引用,该类需要一个类型的方法。 每个表单都代表不同的类型,所以我认为我可以按照以下方式做一些事情: public partial class Base : Form where T : BaseClass { private GenericHandler handler = new GenericHandler(); } public class BaseClass { } public class GenericHandler { public void DoSomethingWithInstance(T instance) where T : BaseClass { } } 我的设计师类声明也反映了我的表单。 现在,当我执行代表Foo类型的第二个表单时,我无法访问设计器,因为我收到此错误: 无法为此文件显示设计器,因为其中的任何类都无法设计。 设计者检查了文件中的以下类:Foo —无法加载基类“WindowsFormsApplication1.Base”。 确保已引用程序集并且已构建所有项目。 FooClass —无法设计基类’WindowsFormsApplication1.BaseClass’。 public partial class Foo : […]

方法隐藏如何在C#中起作用? (第二部分)

打印以下程序 A:C(A,B) B:C(A,B) (正如它应该) public interface I { string A(); } public class C : I { public string A() { return “A”; } public string B() { return “B”; } } public class A { public virtual void Print(C c) { Console.WriteLine(“A:C(” + cA() + “,” + cB() + “)”); } } public class […]

在MVC4中显示和错误,我必须实现一些接口,但我已经完成了它

我正在尝试创建自己的filter属性,以支持多语言。 这个想法很简单。 URL代表语言。 * http://host.ext/en/ rest_of_the_url *将以英文和 * http://host.ext/ hy / rest_of_the_url *将以亚美尼亚语开放。 问题是在运行时它会说MultilingualActionFilterAttribute 以下是错误文本“给定的filter实例必须实现以下一个或多个filter接口:IAuthorizationFilter,IActionFilter,IResultFilter,IExceptionFilter”。 在这里,我将它用作全局filter。 namespace TIKSN.STOZE.WebApp { public class FilterConfig { public static void RegisterGlobalFilters(System.Web.Mvc.GlobalFilterCollection filters) { filters.Add(new TIKSN.STOZE.Common.MultilingualActionFilterAttribute()); filters.Add(new System.Web.Mvc.HandleErrorAttribute()); } } } 我在这里定义它。 namespace TIKSN.STOZE.Common { public class MultilingualActionFilterAttribute : System.Web.Mvc.ActionFilterAttribute { public override void OnActionExecuting(System.Web.Mvc.ActionExecutingContext filterContext) { string language = […]

我如何从Dictionaryinheritance?

我想要Dictionary所有function,但我希望它为Foo 。 我该怎么做呢? 目前我正在使用 class Foo : Dictionary { /* I’m getting all sorts of errors because I don’t know how to overload the constructors of the parent class. */ // overloaded methods and constructors goes here. Foo():base(){} Foo(int capacity):base(capacity){} } 重载父类的构造函数和方法的正确方法是什么? 注意:我认为我滥用了“过载”一词,请更正或建议更正。

entity framework6 – 基类的inheritance和导航属性

我的导航属性和inheritance有问题。 这是我的问题:我有一个基类Person类和inheritance自Person User和Worker类。 在数据库级别,我使用单表inheritance或每层次表(TPH)inheritance。 所以有一个带有鉴别器列的表。 User和Worker需要有Company关系,所以我想在Person类上定义它。 我这样定义我的模型: [Table(“mydb.person”)] public abstract partial class Person { [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] public long ID { get; set; } public long? CompanyID { get; set; } [ForeignKey(“CompanyID”)] public virtual Company Company { get; set; } … } public partial class User : Person { … } public partial class Worker : […]

返回后代的单例实例

我有几个单例类,所以我尝试用方法GetInstance(params)和派生类创建一个父BaseClass,它应该实现这个方法并返回自己的实例(所以我不必抛出它们)… as它们是单例,方法应该是静态的,但不允许覆盖静态方法。 编码它的最佳方法是什么? 我想要的示例代码: public class Base { public static virtual T GetInstance() where T : class; } public class Derived { Derived instance; public static override T GetInstance() where T : typeOf(this){ if (instance == null) { instance = new Derived(); } return instance; } } 在我想要打电话的外面的代码中 Derived.GetInstance().SomeDerivedMethod() 不 (Derived.GetInstance() as Derived).SomeDerivedMethod() and not […]

当多重inheritance不是一个选项时,如何重用代码?

我想利用我几个旧测试类中的一些方法进入我正在构建的新类中。 不幸的是,C#不支持多重inheritance。 如何重用这些旧类中的代码? 我只是将它们创建为成员对象吗? 或者我还有其他选择吗?

如何覆盖更丰富类型的接口方法中定义的参数?

我有这些: public class TennisPlayer { } public class RogerFederer : TennisPlayer { } public class RafaelNadal : TennisPlayer { } 然后我有一些方法类,如下所示: public abstract class Manager { protected abstract void ScheduleFriendlies(TennisPlayer player); } public class RafaelNadalManager : Manager { public void ScheduleFriendlies(RafaelNadal rn) { //throw new NotClayException(); } } public class RogerFedererManager : Manager { public […]