java.util.Vector
有方法:
删除(int索引)
和
删除(对象o)
我有:
vector<Integer> a;
int b=3;
我想要:
使用 b
变量调用方法 remove(Object o)
。
编写 a.remove(b)
显然会调用 remove(int index)
提前致谢,Maciej
最佳答案
a.remove(Integer.valueOf(b));
应该可以。在自动装箱被认为调用 remove(int)
之前,Integer
将首先被解析为引用类型,并匹配 remove(Object)
。
来自Java Language Spec, 15.2.2 :
Compile-Time Step 2: Determine Method Signature
The first phase (§15.12.2.2) performs overload resolution without permitting boxing or unboxing conversion, or the use of variable arity method invocation. If no applicable method is found during this phase then processing continues to the second phase.
The second phase (§15.12.2.3) performs overload resolution while allowing boxing and unboxing...
关于java - 使用不太具体的参数调用重载方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15417512/