我现在三个类A、B、C,还有一个A、B、C要共同实现在接口I,代码框架大概如下:
public interface I
{
//控件类型
ItemControlType ControlType{get; set;}
//是否在编辑状态
bool IsEditing{get;set;}
...
}public class A:System.Windows.Forms.TextBox,I
{
private ItemControlType type
//控件类型
ItemControlType ControlType
{
get{return type;}
set{type = value;}
}
private bool edit
//是否在编辑状态
bool IsEditing
{
get{return edit;}
set{edit= value;}
}}public class B: System.Windows.Forms.ComboBox,I
{
private ItemControlType type
//控件类型
ItemControlType ControlType
{
get{return type;}
set{type = value;}
}
private bool edit
//是否在编辑状态
bool IsEditing
{
get{return edit;}
set{edit= value;}
}
}public class C: System.Windows.Forms.ComboBox,I
{
private ItemControlType type
//控件类型
ItemControlType ControlType
{
get{return type;}
set{type = value;}
}
private bool edit
//是否在编辑状态
bool IsEditing
{
get{return edit;}
set{edit= value;}
}
}我要在调用程序里判断控件是什么类型,再调用相应的方法或属性,请问如上的两个公用属性ControlType及IsEditing能否只写一次,不必每个继承类都写一次,有什么方法,还请大侠不怜赐教.......

解决方案 »

  1.   

    abstract class IBase : I
    {
    }
    abstract class ComboBoxBase : ComboBox
    {
        protected IBase ibase;
    }
    class A : ComboBoxBase
    {
    }
    class B : ComboBoxBase
    {
    }
    class C : ComboBoxBase
    {
    }dearymz@163.com
    欢迎大家邮件讨论(地址请不要复制,我敲的是全角)
      

  2.   

    不好意思,有个地方写错了 C类应为:
    public class C: System.Windows.Forms.CheckBox,I 

    private ItemControlType type 
    //控件类型 
    ItemControlType ControlType 

    get{return type;} 
    set{type = value;} 

    private bool edit 
    //是否在编辑状态 
    bool IsEditing 

    get{return edit;} 
    set{edit= value;} 

    } 我要在调用程序里通过ControlType属性判断控件是什么类型,再调用相应的其它方法或属性,IsEditing能否只写一次,不必每个继承类都写一次,楼上解决方法,好象没法解决这个问题,因为A、B、C必须分别继承不同的控件类TextBox、ComboBox、CheckBox,还要继承公用的方法或属性,这些公用的方法或属性可以写到父类或父接口里,但我还想不出方法去解决,期望大家提议,看有什么好的方法
      

  3.   


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;namespace WindowsFormsApplication1
    {
        interface IBase
        {
            bool isEditing();
        }
        class Class1<T> :IBase
            where T:System.Windows.Forms.Control
        {
            public bool isEditing() { return true; }
        }
        class Class2 : Class1<System.Windows.Forms.CheckBox>
        {
        }
        class Class3 : Class1<System.Windows.Forms.TextBox>
        {
        }
    }
      

  4.   

    .net3.5 有个方法到可以实现那就是Extension Methods(扩展方法),你可以轻松快捷地扩展已有类型的方法定义和实现的机制,但只能是方法。例如
    public static class ExtensionDemo{
    public static bool IsEditing(this System.Windows.Form.Contorl control)
    {
     return true;
    }
    这样就在System.Windows.Form.Contorl 类中增加了IsEditing方法,所有继承此System.Windows.Form.Contorl都有此方法