如何从另一个班级处置?

问题描述 投票:1回答:1

我有一个实现ApplicationLister的主类和一堆没有实现任何东西的其他类,我想到的是这个。

//I create a method for is disposing on my other classes
 public void disposable(){
  //things to dispose
}

// and then call the method on the main class

public void dispose(){
  classObj.disposable();
}

我的主意好吗?以及我如何知道所有可弃用的类/方法。

java libgdx dispose
1个回答
1
投票

libgdx中有一个接口,可让您实施dispose方法。它使您可以将所有一次性用品放入列表,并在最后处理它们。最后,在拥有资源的每个对象上实现处置不是一个坏主意,例如Assetmanager或Screen实现。但是您并不需要所有的东西,因为当垃圾收集器在对象上运行时,对象确实会被破坏。您不能故意删除对象。

查看Disposable Interface和实现此功能的类,以了解可以处理的类。如前所述,它用于保存资源的类。

一个简单的类如下:

public class Foo implements Disposable{
    @override
    public void dispose()
    {
        //release the resources used here like textures and so on
    }
}

确实看起来像您的方法,但是您可以将所有一次性物品添加到列表中,以便在游戏关闭时进行处置:

ArrayList<Disposable> disposables = new ArrayList<Disposable>();
Foo myFoo = new Foo();
disposables.add(myFoo);

//game is running
//....
//
for(Disposable d : myFoo)
{
    d.dispose();
}
//end of the main

尝试使用Libgdx util类。

进一步阅读有关处置及其原因的知识:Memory Management from libgdx Wiki

来自这里的一个导入和检测是:

[...]实现一个通用的Disposable接口,该接口指示该类的实例需要在manually的结尾处进行处理。一生。

© www.soinside.com 2019 - 2024. All rights reserved.