C#From继承接口和抽象类. [英] C# From inherit both interface and abstract class.

查看:84
本文介绍了C#From继承接口和抽象类.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

长话短说.我想创建许多窗口(Form类),ofc必须继承Form.cs类.但我也想,每个窗口都将实现接口(基本上,是要实现的必要方法的列表)并继承抽象类(因此,某些方法(必需或不需要)将具有默认行为).

像这样:Interface->抽象类-> MyForm(继承Form类).

您能告诉我实现这种组合的最佳方法是什么吗?

Hi, Long story short.. I would like to create many windows (Form classes), that ofc has to inherit Form.cs class. But I want also, that every window will implement interface (so basically, list of necessary methods to implement) and inherit abstract class (so some of methods (necessary or not), will have default behavior).

Sth like that: Interface -> Abstract class -> MyForm (which inherits Form class).

Could you tell me what is the best way to implements such a combination ?

推荐答案

只有一种方法:创建一个继承Form的抽象类,并派生您的从那形成.然后,它也可以实现接口.

您不能同时从抽象类和Form类派生-继承层次结构仅允许一个基类,无论抽象与否.

顺便说一句:直接从抽象类继承之前,请仔细看一下google,否则您将开始发誓. VS设计器不允许您显示基于抽象类的表单(或其他控件)-您必须在将其继承表单之前将其派生为非抽象类. :mad:

[edit]添加了VS设计器行为-OriginalGriff [/edit]
There is only one way: to create an abstract class that inherits Form, and derive your form from that. It can then also implement the interfaces.

You cannot derive from an abstract class and the Form class together - the inheritance hierarchy only allows the one base class, abstract or not.

BTW: Have a good look at google before you inherit directly from the abstract class, or you will start swearing. The VS designer will not allow you to display forms (or other controls) based on abstract classes - you have to derive it to a non-abstract class before your form inherits it. :mad:

[edit]Added VS designer behaviour - OriginalGriff[/edit]


请参阅我的评论.

我的意思是,尽管如此,我还是可以分享一种非常有益的编码样式,以实现某些接口,尤其是表单.这种样式基于这样的事实,即部分类声明只需要基类或接口列表中的每个元素一次.这意味着,您不应重复其中的任何一个.这使您有机会分离课程的不同方面.特别是,与每个接口的实现有关的部分可以位于单独的文件中.像这样:

Please see my comment.

I mean, on second though, I can share a very beneficial coding style, when it comes to implementing some interfaces, especially good with forms. This style is based on the fact that partial class declaration requires each element in the list of base classes or interfaces only once. It means, you should not to repeat any of them. This gives you the opportunity to separate different aspects of the class. In particular, the parts related to implementation of each interface, could be in separate files. Like this:

public partial class MyForm : System.Windows.Forms.Form { /* ... */ }

internal interface IFirst { /* ... */ } // practically, never public...

internal interface ISecond { /* ... */ } //...because forms are rarely accessed from another assembly

public partial class MyForm : IFirst { /* implement IFirst here... */ } //no need to mention base class Form or ISecond

public partial class MyForm : ISecond { /* implement ISecond here... */ } //no need to mention base class Form or IFirst



-SA



—SA


public abstract class MyAbstractClass : Form, MyInterface
    { 
        public MyAbstractClass()
        { 
        }
    }
    public interface MyInterface
    { 
    
    }
    public class MyFormClass : MyAbstractClass
    {
        public MyFormClass()
        { 
        
        }
    }


这篇关于C#From继承接口和抽象类.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆