简体   繁体   English

实现相同属性的C#类

[英]C# classes that implement the same properties

I have a couple classes that have the same properties. 我有几个具有相同属性的类。

For example 例如

public class Decimalclass
{
   public string SqlColumnName {get;set;}
   public SqlDbType SqlColumnType {get;set;}
   public Decimalclass()
   {
      SqlColumnType = SqlDbType.Decimal;
   }
   //...
}

public class Textclass
{
  public string SqlColumnName {get;set;}
  public SqlDbType  SqlColumnType {get;set;}
  public Textclass()
   {
      SqlColumnType = SqlDbType.NVarChar;
   }
   //...
}

public class Intclass
{
  public string SqlColumnName {get;set;}
  public SqlDbType  SqlColumnType {get;set;}
  public Intclass()
   {
      SqlColumnType = SqlDbType.Int;
   }
   //...
}

As you can see those classes share the same properties, I am trying to learn about interfaces and abstract classes. 如您所见,这些类共享相同的属性,我试图了解接口和抽象类。

  • How would you organize those classes by making an interface that holds the things they share? 您如何通过建立一个包含它们共享的东西的界面来组织这些类?
  • Is an interface the best way? 接口是最好的方法吗?
  • Can I add those classes to a list of the type of the interfaces so that I can access the properties without having to cast? 是否可以将这些类添加到接口类型列表中,以便无需强制转换即可访问属性?
  • Why an interface and why an abstract class, what advantages they have overeachother 为什么要使用接口以及为什么要使用抽象类,它们比其他方法有什么优势

I would make the SqlColumnType abstract and read-only to force implementing it in derived classes. 我将使SqlColumnType抽象和只读,以强制在派生类中实现它。

public abstract class BaseClass
{
    public string SqlColumnName { get; set; }
    public abstract SqlDbType SqlColumnType { get; }
}

public class Intclass : BaseClass
{
    public override SqlDbType SqlColumnType
    {
        get { return SqlDbType.Int;  }
    }
}

I would do : 我会做 :

public abstract class BaseClass
{
  public string SqlColumnName {get;set;}
  public SqlDbType  SqlColumnType {get;set;}
}

public class Intclass : BaseClass
{
   public Intclass()
   {
      base.SqlColumnType = SqlDbType.Int;
   }
}

Updated to better answer the OPs Q 更新以更好地回答运营问题Q

The Interface specifies a contract that must be followed on the object implementing the interface. 接口指定实现接口的对象必须遵循的协定。 Whilst the abstract base class provides a method to implement the interface automatically in all objects that inherit from it. 虽然抽象基类提供了一种方法,以在从其继承的所有对象中自动实现接口。

    interface IBase
    {
         string SqlColumnName { get; set; }
         SqlDbType SqlColumnType { get; set; }
    }

    public abstract class BaseClass : IBase
    {
        public string SqlColumnName { get; set; }
        public SqlDbType SqlColumnType { get; set; }
    }

    public class Intclass : BaseClass
    {
        public Intclass()
        {
            base.SqlColumnType = SqlDbType.Int;
        }
    }

So in that example the interface IBase says that all implementer must contain these two properties to meet the contract. 因此,在该示例中,接口IBase表示所有实现者都必须包含这两个属性才能满足合同要求。 This is useful especially when following an Inversion of Control IoC or Dependency Injection pattern. 这在遵循控制IoC转换或依赖项注入模式时尤其有用。 This allows you to implement the interface on new objects and maintain compatibility anything that takes an IBase as an argument. 这样,您就可以在新对象上实现该接口,并保持兼容性(以IBase为参数)。 The abstract class implements the interface which is then inherited by any object that inherits from the base class. 抽象类实现了接口,然后该接口将由从基类继承的任何对象继承。 Basically by using the abstract base class you don't have to specifically implement each property in your child objects. 基本上,通过使用抽象基类,您不必专门在子对象中实现每个属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM