flex – 弱引用和强引用
package uk.co.bigroom.utils { import flash.utils.Dictionary; /** * Class to create a weak reference to an object. A weak reference * is a reference that does not prevent the object from being * garbage collected. If the object has been garbage collected * then the get method will return null. */ public class WeakRef { private var dic:Dictionary; /** * The constructor - creates a weak reference. * * @param obj the object to create a weak reference to */ public function WeakRef( obj:* ) { dic = new Dictionary( true ); dic[obj] = 1; } /** * To get a strong reference to the object. * * @return a strong reference to the object or null if the * object has been garbage collected */ public function get():* { for ( var item:* in dic ) { return item; } return null; } } } 在这个类中,它们如何表示一个弱引用和一个作为强引用. 解决方法
类本身模拟弱引用,可以作为一个传递.
如果没有对该值的其他弱引用,则它是垃圾收集的,因为对它的引用是通过具有弱(引用)键的字典建立的. 只要获得其中的值,就会获得对象本身的引用,就像ActionScript中的所有直接对象引用一样,它是一个强引用.只要您保留该引用,该对象就不会被垃圾回收. 编辑:弱引用和强引用之间的区别 格尔茨back2dos (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |