有没有办法强制转换对象的方法值?我试过这种方式,但它在"instanceof"部分中给出了一个编译时异常:
public static <T> T convertInstanceOfObject(Object o) {
if (o instanceof T) {
return (T) o;
} else {
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
我也试过这个,但它给出了一个运行时异常,ClassCastException:
public static <T> T convertInstanceOfObject(Object o) {
try {
T rv = (T)o;
return rv;
} catch(java.lang.ClassCastException e) {
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
是否有可能轻松地做到这一点:
String s = convertInstanceOfObject("string");
System.out.println(s); // should print "string"
Integer i = convertInstanceOfObject(4);
System.out.println(i); // should print "4"
String k = convertInstanceOfObject(345435.34);
System.out.println(k); // should print "null"
Run Code Online (Sandbox Code Playgroud)
编辑:我写了正确答案的工作副本:
public static <T> T …Run Code Online (Sandbox Code Playgroud) 对于类变量,向上铸造和向下铸造有什么区别?
例如,在下面的程序类中,Animal只包含一个方法但Dog类包含两个方法,那么我们如何将Dog变量转换为Animal变量.
如果完成了施法,那么我们如何使用Animal的变量调用Dog的另一种方法.
class Animal
{
public void callme()
{
System.out.println("In callme of Animal");
}
}
class Dog extends Animal
{
public void callme()
{
System.out.println("In callme of Dog");
}
public void callme2()
{
System.out.println("In callme2 of Dog");
}
}
public class UseAnimlas
{
public static void main (String [] args)
{
Dog d = new Dog();
Animal a = (Animal)d;
d.callme();
a.callme();
((Dog) a).callme2();
}
}
Run Code Online (Sandbox Code Playgroud) 在以下示例中
int i = -128;
Integer i2 = (Integer) i; // compiles
Integer i3 = (Integer) -128; /*** Doesn't compile ***/
Integer i4 = (Integer) (int) -128; // compiles
Integer i4 = -128; // compiles
Integer i5 = (int) -128; // compiles
Integer i6 = (Integer) (-128); // compiles
Integer i7 = (Integer) 0-128; // compiles
Run Code Online (Sandbox Code Playgroud)
我不能施展-128,(Integer)但我可以施展(int) -128.
我一直认为它-128是int类型,并且它(int)应该是多余的.
行上的错误i3是
cannot find symbol variable Integer
Run Code Online (Sandbox Code Playgroud)
我尝试使用Java …
在PostgreSQL中,我有一个带varchar列的表.数据应该是整数,我在查询中需要整数类型.有些值是空字符串.下列:
SELECT myfield::integer FROM mytable
Run Code Online (Sandbox Code Playgroud)
产量 ERROR: invalid input syntax for integer: ""
如何在postgres中投射期间查询演员并且在出现错误时为0?
我通常更喜欢编码R,以便我不会收到警告,但我不知道在使用as.numeric转换字符向量时如何避免收到警告.
例如:
x <- as.numeric(c("1", "2", "X"))
Run Code Online (Sandbox Code Playgroud)
会给我一个警告,因为它通过强制引入了NA.我希望通过强制来引入NA - 有没有办法告诉它"是的,这就是我想做的事情".或者我应该接受警告?
或者我应该使用不同的功能来执行此任务?
我可以假设(bool)true == (int)1任何C++编译器吗?
我从一个在Varchar中有原始feed的表中导入数据,我需要将varchar中的一列导入到一个字符串列中.我尝试使用<column_name>::integer以及to_number(<column_name>,'9999999')但是我收到错误,因为有一些空字段,我需要将它们检索为空或null到新表中.
如果有相同的功能,请告诉我.
我有以下查询,需要转换id为varchar
架构
create table t9 (id int, name varchar (55));
insert into t9( id, name)values(2, 'bob');
Run Code Online (Sandbox Code Playgroud)
我尝试了什么
select CAST(id as VARCHAR(50)) as col1 from t9;
select CONVERT(VARCHAR(50),id) as colI1 from t9;
Run Code Online (Sandbox Code Playgroud)
但他们不起作用.请建议.
在Java中,我想将double转换为整数,我知道如果你这样做:
double x = 1.5;
int y = (int)x;
Run Code Online (Sandbox Code Playgroud)
你得到y = 1.如果你这样做:
int y = (int)Math.round(x);
Run Code Online (Sandbox Code Playgroud)
你可能会得到2.但是,我想知道:因为整数的双重表示有时看起来像1.9999999998或其他东西,是否有可能通过Math.round()创建一个双重表示仍然会导致截断的数字,而不是比我们正在寻找的舍入数字(即代码中代码为1而不是2)?
(是的,我的意思是这样的:x 是否有任何值,其中y将显示一个截断的结果而不是x的圆形表示?)
如果是这样的话:有没有更好的方法将double变为圆形int而不会有截断的风险?
想象一下:Math.round(x)返回一个long,而不是double.因此:Math.round()不可能返回一个看起来像3.9999998的数字.因此,int(Math.round())将永远不需要截断任何内容并始终有效.