我是否需要在我的对象中实现处置或最终化?

编程入门 行业动态 更新时间:2024-10-27 04:28:40
本文介绍了我是否需要在我的对象中实现处置或最终化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

可悲的是,它从未成为一个问题......所以我永远不会变成一个问题给了这个问题的第二个想法。

现在当我想到它时,我并不真正了解dispose函数的真正含义以及它应该如何以及何时应该

同样的问题可以敲定...

最后一个问题... 我有一个类pictureManipulation:当我需要保存/调整大小/更改格式...我启动该类的一个新实例使用它的对象和...让垃圾收集杀死实例

class student { public void displayStudentPic() { PictureManipulation pm = new PictureManipulation(); this.studentPic = pm.loadStudentImage(id); } } 类测试 {学生a = new Student(); a.displayStudentPic(); //现在函数执行结束了...... pm对象是否死了? GC会杀死它吗?

解决方案

c> class Student

我需要一个 Dispose()假设图片类是IDisposable:是。因为一个Student对象'拥有' studentPic 并且负责清理它。最简单的实现:

class Student:IDisposable { private PictureClass studentPic; public void Dispose() { if(studentPic!= null) studentPic.Dispose(); } ... }

你现在使用一个Student对象,例如:

void Test { using(Student a = new Student()) { a.displayStudentPic(); } // auto使用()}

如果可以't /不要使用 using(){} block,只需调用 a.Dispose(); when你已经完成了。 但是请注意,这里的(更好的)设计会避免将一个图片对象留在您的Student对象中。这引发了一整套责任。

我需要一个Finalizer吗?

否。因为当一个Student对象被收集时,它的studentPic对象被保证在同一个运行中被收集。 Finalizer(析构函数)将毫无意义,但仍然很昂贵。

For too long I let the garbage collector do its magic, removing all responsibilities from my self.

Sadly it never turned into an issue... So I never gave a second thought to the subject.

Now when I think about it I don't really understand what the "dispose" function really does and how and when it should be implemented.

The same question for finalize...

And a last question... I have a class pictureManipulation : when I need to save/resize/change format ... I start a new instance of that class use its objects and... well let the garbage collection kill the instance

class student { public void displayStudentPic() { PictureManipulation pm = new PictureManipulation(); this.studentPic = pm.loadStudentImage(id); } } Class Test { student a = new Student(); a.displayStudentPic(); // Now the function execution is ended... does the pm object is dead? Will the GC will kill it? }

解决方案

Regarding your class Student

Do I need a Dispose() ?

Assuming the Picture class is IDisposable: Yes. Because a Student object 'owns' the studentPic and that makes it responsible for cleaning it up. A minimal implementation:

class Student : IDisposable { private PictureClass studentPic; public void Dispose() { if (studentPic != null) studentPic.Dispose(); } ... }

And you now use a Student object like:

void Test { using (Student a = new Student()) { a.displayStudentPic(); } // auto Dispose by using() }

If you can't/don't use a using(){} block, simply call a.Dispose(); when you're done with it.

But please note that the (far) better design here would be to avoid keeping a picture object inside your Student object. That sets off a whole chain of responsibilities.

Do I need a Finalizer?

No. Because when a Student object is being collected, its studentPic object is guaranteed to be collected in the same run. A Finalizer (destructor) would be pointless but still expensive.

更多推荐

我是否需要在我的对象中实现处置或最终化?

本文发布于:2023-11-27 08:46:33,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:象中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!