使用ArrayList< Class?>铸造? [英] Using an ArrayList<Class?> for casting?

查看:244
本文介绍了使用ArrayList< Class?>铸造?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上一个问题



我有以下代码:

  ArrayList< Object> list = new ArrayList< Object>(); 
list.add(StringType);
list.add(5);
list.add(new RandomClass());

List< Class<?>> classes = new ArrayList<>();
classes.add(String.class);
classes.add(int.class);
classes.add(RandomClass.class);

for(int i = 0; i if(classes.get(i).isInstance(list.get(i))) {
...
}
}

if(isvalid)
mymethod(...);


public void mymethod(String string,int num,RandomClass randomClass){}


b $ b

现在我试图使用一个使用字符串参数的方法将对象转换为正确的类型。



而不是:

  mymethod((String)list.get(0),(int)list.get(1),(RandomClass)list.get ); 

我想重复使用上面创建的定义。

  mymethod((define.get(0))list.get(0),....); 



我也尝试过使用Class.cast(obj),但是当然它返回一个类型' ?

解决方案

什么是类型安全? / strong>


在计算机科学中,类型安全是编程
语言阻止或防止类型错误的程度。 / p>

如果代码是类型安全的,那么编译器可以在编译时验证所有类型都是正确的:

  String getName(){
returnname;
}



编译器知道name必须是



一旦你这样做:

$ b $

  int getNumber(){
(int)number;
}

需要显式转换为 int 告诉你这个代码有一个错误条件,即当 number 不是类型 int



它会如何影响你? p>

您的代码:

  define.get(0).cast .get(0))

您希望此语句的返回类型为 get(0)。但编译器没有办法知道,在编译时, define.get(0)返回。



您有清单< Class<?>> ,即一个我不在乎什么类型类的 List 。然后,您使用此列表的成员来投射您的其他列表的成员 - 唯一的结果可以是我不在乎什么类型。



您可以使用以下方法来实现:

 code>< T> T get(final int i){
return(T)define.get(i).cast(list.get(i));
}

这将很乐意编译:

  final String thing = get(0); 

同样:

  final int thing = get(0); 

你所做的一切都是内生的演员。错误情况仍然存在。


Previous question

I have the following code:

ArrayList<Object> list = new ArrayList<Object>();
list.add("StringType");
list.add(5);
list.add(new RandomClass());

List<Class<?>> classes = new ArrayList<>();
classes.add(String.class);
classes.add(int.class);
classes.add(RandomClass.class);

for (int i = 0; i < list.size(); i++) {
    if (classes.get(i).isInstance(list.get(i))) {
        ...
    }
}

if (isvalid)
 mymethod(...);


public void mymethod(String string, int num, RandomClass randomClass){ }

Now I'm trying to cast the object into the right type with a method using a string argument.

Instead of:

mymethod( (String) list.get(0), (int) list.get(1), (RandomClass) list.get(2) );

I would like to reuse the definition created above for the cast.

mymethod( ( define.get(0) ) list.get(0), .... );

I've also tried using the Class.cast(obj) but of course it returns a type '?' which again defeats the purpose of casting it again using (String).

解决方案

What is type safety?

In computer science, type safety is the extent to which a programming language discourages or prevents type errors.

If code is type safe, then the compiler can verify, at compile time, that all the types are correct:

String getName() {
    return "name";
}

The compiler knows that "name" must be a String so it can verify that this code will never throw a type error.

Once you do something like:

int getNumber() {
    (int) number;
}

The need to explicitly cast to int tells you that this code has an error condition, namely when number is not of type int or a type that is assignable to int.

How does it affect you?

Your code:

define.get(0).cast(list.get(0))

You want the return type of this statement to be of the type of get(0). But the compiler has no way of knowing, at compile time, what define.get(0) returns. This is inidcated to you by the return type.

You have a List<Class<?>>, i.e. a List of a class of "I don't care what type". You then use a member of this List to cast a member of your other List - the only result can be an "I don't care what type".

You can hack around this with:

<T> T get(final int i) {
    return (T) define.get(i).cast(list.get(i));
}

This will happily compile:

final String thing = get(0);

As will:

final int thing = get(0);

i.e. all that you have done is to endogenise the cast. The error condition still exists.

这篇关于使用ArrayList&lt; Class?&gt;铸造?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆