c# - 公共(public)密封类 SqlConnection : DbConnection, ICloneable

标签 c# .net oop multiple-inheritance sqlconnection

请帮助我理解以下内容:

public sealed class SqlConnection : DbConnection, ICloneable {...}

在上面的课上我有两个疑惑

  1. 在 C# 中,多重继承是不可能的(我们可以通过接口(interface)实现)。但是这里的 DBconnection 不是一个接口(interface)那么它是如何支持多重继承的呢?

  2. Iclonable 是一个接口(interface)。它有一个名为 object Clone() 的方法。但是在 Sqlconnection 类中没有实现该方法。怎么可能?

请帮助我理解这一点

最佳答案

  1. 这里没有多重继承。您可以继承一个类并实现多个接口(interface)。这里,DBConnection 是一个类,IClonable 是一个interface

  2. IClonable 声明为 Explicit Interface ,这意味着您不能直接从类实例访问它,而必须显式转换为接口(interface)类型

示例

interface IDimensions 
{
     float Length();
     float Width();
}

class Box : IDimensions 
{
     float lengthInches;
     float widthInches;

     public Box(float length, float width) 
     {
        lengthInches = length;
        widthInches = width;
     }

     // Explicit interface member implementation: 
     float IDimensions.Length() 
     {
        return lengthInches;
     }

    // Explicit interface member implementation:
    float IDimensions.Width() 
    {
       return widthInches;      
    }

 public static void Main() 
 {
      // Declare a class instance "myBox":
      Box myBox = new Box(30.0f, 20.0f);

      // Declare an interface instance "myDimensions":
      IDimensions myDimensions = (IDimensions) myBox;

      // Print out the dimensions of the box:
      /* The following commented lines would produce   compilation 
       errors because they try to access an explicitly implemented
       interface member from a class instance:                   */

      //System.Console.WriteLine("Length: {0}", myBox.Length());
    //System.Console.WriteLine("Width: {0}", myBox.Width());

    /* Print out the dimensions of the box by calling the methods 
     from an instance of the interface:                         */
    System.Console.WriteLine("Length: {0}", myDimensions.Length());
    System.Console.WriteLine("Width: {0}", myDimensions.Width());
    }
 }

关于c# - 公共(public)密封类 SqlConnection : DbConnection, ICloneable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23843786/

相关文章:

c# - 如何拦截WCF Web方法请求?

c# - 北风数据集

c# - WCF 跟踪上未找到配置评估上下文警告

.net - 为什么PS抓不到: "New-PSDrive : Logon failure: unknown user (...)"?

oop - 仅实例化一个类的唯一对象

C++:基类调用自己的虚函数——一种反模式?

c# - 如何使用运行时发生变化的 WebElement?

c# - FileInfo.Delete 中的异常 - IOException

.net - 为什么 System.Object 在 .NET 中不是抽象的?

c# - 如何在 .NET 中获取给定字符串的单向哈希?