哎呀,抽象类中的问题 [英] Question in oops,in abstract class

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

问题描述

1我们可以在抽象类中添加析构函数吗?
2在哪里使用抽象类和接口?
请提供示例.

在此先感谢.

1 Can we add destructor in abstract class?
2 Where to use abstract class and interface ?
Please provide example.

Thanks in Advance.

推荐答案


#1:
当然,您可以 编写 抽象类中的析构函数.这是完全合理的,因为析构函数是继承的派生类,并且在销毁过程中会被实际调用.如果不在层次结构中的某个地方创建和实例化非抽象类,就无法使用它,因为您无法实例化一个抽象类.现在,由于.NET具有托管内存和垃圾收集器(GC),因此在.NET中编写析构函数毫无意义.这在某些方面是不平凡的. GC标记了所有无法从当前正在运行的代码访问的对象,并最终破坏了它们并回收了它们的内存,但是它不保证发生任何时间.使用.NET,析构函数不需要太多工作.我建议您不要使用,除非您知道自己的情况确实很好.

#2:
这个问题被问了很多遍.我想展示旧答案,因为它们很有趣,也很讨论.我已经给出了答案,但也请参见其他内容.

请参阅有关如何决定使用接口还是抽象类的讨论:
#1:
Of course you can write a destructor in abstract class. It makes perfect sense, because the destructor is inherited be a derived class and will be actually called during its destruction. You cannot literally use it without creating and instantiating non-abstract class somewhere down the hierarchy, because you cannot instantiate an abstract class. Now, writing destructors in .NET makes little sense because of its managed memory and Garbage Collector (GC). This is somewhat non-trivial aspect. GC marks all objects which cannot be accessed from currently running code and eventually destructs them and reclaim their memory, but it does not guarantee anything about the moment of time when it happens. With .NET, there is no much work for destructors. I would recommend not using unless you know what are your doing really well.

#2:
The question is asked many times. I want to show old Answers, because they are interesting, as well as discussion. I already gave my Answer, but see for others as well.

Please see the discussion on how to decide on use of interfaces vs. abstract classes: How to decide to choose Abstract class or an Interface[^].

Also, one fundamental difference is that interfaces allows for multiple inheritance, in contrast to classes (abstract or not). This is called weak form of multiple inheritance and cause serious design implications.

—SA


请问我能给我代码来覆盖析构函数吗?"

您不会以与其他方法相同的方式重写-您只需将自己的终结器添加到派生类
"Plz can u give me the code to override the destructors"

You don''t override in the same way as another method - you just add your own finalizer to the derived class
public class DerivedClass : BaseClass
{
    ~DerivedClass()
    {
        // destructor code here
    }
}


除非需要进行一些非托管资源清理,否则很少需要终结器.如果您使用它们,我建议您使用处置模式".


Unless there is some unmanaged resource clean up required, you rarely need finalizers. If you are using them I would recommend using the Dispose Pattern.

public abstract class BaseClass : IDisposable
{
    // if not reusable once disposed then need to track the state
    private bool disposed;

    ~BaseClass()
    {
        Dispose(false);
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this); 
    }
    protected virtual void Dispose(bool disposing)
    {
        if(!disposed)
        {
            disposed = true;
            if(disposing)
            {
                // managed cleanup
            }
            // unmanaged cleanup
        }
    }
}


这篇关于哎呀,抽象类中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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