小编Jas*_*onF的帖子

Java Auto Cast一个对象

我想知道是否有办法通过将Class类型与对象一起存储来自动将对象转换为某种类型?我认为这可能是Java,但也许不是.

例如:

class StorageItem
{
    private int itemcount;

    StorageItem(int itemcount)
    {
        this.itemcount = itemcount;
    }

    int getItemCount()
    {
        return itemcount;
    }
}

class Storage
{
    private Class clazz;

    private Object value;

    public Storage(Class clazz, Object value)
    {
        this.clazz = clazz;
        this.value = value;
    }

    //Is there a way such a call can be created to automatically cast 
    //the object to the class type and return that cast type in a 
    //generic way. The idea being that Storage knows what it …
Run Code Online (Sandbox Code Playgroud)

java casting

0
推荐指数
1
解决办法
3854
查看次数

标签 统计

casting ×1

java ×1