sách gpt4 ai đã đi

Nhóm Libgdx cho các loại đối tượng phụ khác nhau

In lại 作者:行者123 更新时间:2023-11-30 03:21:22 30 4
mua khóa gpt4 Nike

我有一个单位类及其一些子类(弓箭手、剑客等)。我怎样才能创建一个回收所有单元类型子类的池?

1 Câu trả lời

这是不可能的,因为只能包含一种特定类型的对象。否则你可能会遇到这样的情况:

Pool unitPool = ...;
Archer acher = new Archer();
unitPool.free(archer); // we free an Archer, who is a Unit
Unit swordsmanUnit = unitPool.obtain(); // we can obtain only Units
Swordsman swordsman = (Swordsman) swordsmanUnit; // This is actually an Archer and will result in a ClassCastException

幸运的是,libgdx 附带了一个名为 Pools 的实用程序,可以轻松池化许多不同的类。它为每个类创建一个ReflectionPool,并从正确的池中释放/获取您的对象。只需使您的 UnitPoolable 即可。

Archer archer = Pools.obtain(Archer.class);
Swordsman swordsman = Pools.obtain(Swordsman.class);
// ...
Pools.free(archer);
Pools.free(swordsman);

关于java - 用于不同类型子对象的 Libgdx 池,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31246559/

30 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com