Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 公共密封类SqlConnection:DbConnection,可克隆_C#_.net_Oop_Multiple Inheritance_Sqlconnection - Fatal编程技术网

C# 公共密封类SqlConnection:DbConnection,可克隆

C# 公共密封类SqlConnection:DbConnection,可克隆,c#,.net,oop,multiple-inheritance,sqlconnection,C#,.net,Oop,Multiple Inheritance,Sqlconnection,请帮助我理解以下内容: public sealed class SqlConnection : DbConnection, ICloneable {...} 在上面的课上,我有两个疑问 在C#中,多重继承是不可能的(我们可以通过接口实现)。但是这里DBconnection不是一个接口,那么它是如何支持多重继承的呢 Iclonable是一个接口。它有一个名为object Clone()的方法。但在Sqlconnection类中,该方法未实现。这怎么可能 请帮助我理解这一点 这里没有多重继承。您可

请帮助我理解以下内容:

public sealed class SqlConnection : DbConnection, ICloneable {...}
在上面的课上,我有两个疑问

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

  • Iclonable是一个接口。它有一个名为object Clone()的方法。但在Sqlconnection类中,该方法未实现。这怎么可能

  • 请帮助我理解这一点

  • 这里没有多重继承。您可以从一个类继承并实现多个接口。这里,
    DBConnection
    是一个类,
    IClonable
    是一个
    接口

  • IClonable声明为,这意味着您不能直接从类实例访问它,但必须显式转换为接口类型

  • 示例

    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());
        }
     }
    

    这不是多重继承。正如您正确注意到的,
    DbConnection
    是一个类,
    ICloneable
    是一个接口。只有一个基类,
    DbConnection
    ,因此这是单一继承


    至于
    Clone()
    方法,
    SqlConnection
    确实实现了它,但使用了。这将隐藏
    Clone()
    方法,直到您将
    SqlConnection
    对象视为
    ICloneable
    。当类的作者决定类应该实现一个接口时,类可以这样做,但是该接口提供的方法通常没有意义调用这个特定的类。

    您好,谢谢您的帮助。。。这里您使用了IDiDimensions.Length()对吗?但是Sqlconnection没有类似的东西?您是在类中声明接口吗?对不起,我不明白发生了什么事。在sampleclass内的这个程序中,paint方法是实现的,对吗?但在Sqlconnection类中,Clone()方法并没有实现……如何实现?因为它是一个显式接口实现。您需要将对象强制转换为接口类型才能查看该方法<代码>SampleClass sc=新的SampleClass();IControl ctrl=(IControl)sc谢谢我们必须在Sqlconnection内部实现Iconeable.Clone()方法,对吗?但是在Sqlconnection类中没有类似的实现…它是如何工作的?@saran在
    Sqlconnection
    中有类似的实现。谢谢,它在这个链接中可用。但是当我在Vs2012 Sqlconnection类中搜索时,它不可用。所以我担心this@saran这就是Yuval Itzhakov和我的回答试图解释的:如果你有一个
    SqlConnection con
    变量,你不能调用
    con.Clone()
    ,但是你可以调用
    ((可克隆的)con.Clone()
    )。不使
    con.Clone()
    ((ICloneable)con.Clone()
    相同是显式接口实现的要点。