界面和抽象类混淆 [英] Interface and Abstract class confusion

查看:90
本文介绍了界面和抽象类混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,我可以在课堂上使用多个界面,但我对一些关于此的文章非常困惑。所以这是我的问题: -



我有一个名为driver的抽象类

  public   abstract   class 驱动程序



我用来建立我的司机

  class  Driver_04:驱动程序



我们现在想象一下,我也想要一个Dispose方法;在我看来,我应该能够做到这一点

  class  Driver_04:Driver,IDisposable 
{
...
public 覆盖 无效 Dispose()
{
}

}



然而编译器不会允许这篇文章还建议将IDisposable添加到我的抽象类中,并将方法包含在Dispose中。对我来说这没有任何意义,因为在这种情况下,接口IDisposable是多余的?

有人可以解释一下吗?另外,假设我想要实现的是显而易见的,你会怎么做?

解决方案

  Driver_04:驱动程序,IDisposable 
{
...
public 覆盖 void Dispose()
{
}
}





您不能覆盖Dispose()方法,因为它在基类中不存在。


< blockquote>我有点困惑:

然而编译器不会说这个

是的它会:

  public   abstract   class 驱动程序
{
public abstract void DoIt();
}
public class DDriver:Driver,IDisposable
{
public 覆盖 void DoIt( )
{
throw new NotImplementedException();
}

public void Dispose()
{
throw new NotImplementedException();
}
}

正如我所料,我的系统(VS2010)上没有任何错误或警告干净地编译。



但是,我会这样做:

  public  摘要 驱动程序:IDisposable 
{
public abstract void DoIt();
public abstract void Dispose ();
}
public class DDriver:Driver
{
public 覆盖 void DoIt()
{
throw new NotImplementedException();
}

public 覆盖 void Dispose()
{
throw new NotImplementedException();
}
}

因为那时你强制每个派生类实现IDisposable,这意味着抽象类变量可以在中使用阻止它们包含的最终类。如果在一个Derived类上实现IDisposable,则必须检查并转换每个抽象类实例以查看它是否需要Dispose调用。


  public   abstract   class 驱动程序:IDisposable 
{
public abstract void 处置();
}

public class Driver_04:Driver
{
public 覆盖 void Dispose()
{
// 根据此类的需要使用dispose调用
}
}
public class Driver_03:Driver
{
public 覆盖 void Dispose()
{
// 使用此类所需的dispose调用
}
}


It is my understanding that I can use multiple interfaces with my class but i''m very confused with some of the articles about this. So here''s my issue:-

I have an abstract class called driver

public abstract class Driver


which I use to base my "Drivers" on

class Driver_04 : Driver


Let''s now imagine that I also want to have a "Dispose" method; in my mind, I should be able to do this

class Driver_04 : Driver,IDisposable
{
...
   public override void Dispose()
   {
   }

}


however the compiler wont alow this but also, articles'' suggest that the IDisposable should be added to my abstract class and the method Dispose included too. To me this makes no sense because the interface IDisposable is redundant in this instance ?
Can someone please explain ? Also, assuming it is obvious what I am trying to achieve, how would you do it ?

解决方案

class Driver_04 : Driver,IDisposable
{
...
   public override void Dispose()
   {
   }
}



You cannot override the Dispose() method as it does not exist in the base class.


I''m a little confused:
"however the compiler wont alow this"
Yes it will:

public abstract class Driver
    {
    public abstract void DoIt();
    }
public class DDriver : Driver, IDisposable
    {
    public override void DoIt()
        {
        throw new NotImplementedException();
        }

    public void Dispose()
        {
        throw new NotImplementedException();
        }
    }

Compiles cleanly with no errors or warnings on my system (VS2010) as I would expect.

However, I would do it this way:

public abstract class Driver : IDisposable
    {
    public abstract void DoIt();
    public abstract void Dispose();
    }
public class DDriver : Driver
    {
    public override void DoIt()
        {
        throw new NotImplementedException();
        }

    public override void Dispose()
        {
        throw new NotImplementedException();
        }
    }

Because then you are forcing each derived class to implement IDisposable, which means that the abstract class variables can be used in using blocks irrespective of what eventual class they contain. If you implement IDisposable on one Derived class, then you have to check and convert each abstract class instance to see if it requires a Dispose call.


public abstract class Driver : IDisposable
{
    public abstract void Dispose();
}

public class Driver_04 : Driver
{
    public override void Dispose()
    {
        // use dispose call as needed for this class
    }
}
public class Driver_03 : Driver
{
    public override void Dispose()
    {
        // use dispose call as needed for this class
    }
}


这篇关于界面和抽象类混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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