在某些情况下,使用类型转换返回null值而不是抛出ClassCastException是切实可行的。 C#使用作为运算符来执行此操作。 Java中是否存在等效的东西,因此您不必显式检查ClassCastException?

有帮助吗?

解决方案

这是@Omar Kooheji所建议的as的实现:

public static <T> T as(Class<T> clazz, Object o){
    if(clazz.isInstance(o)){
        return clazz.cast(o);
    }
    return null;
}

as(A.class, new Object()) --> null
as(B.class, new B())  --> B

其他提示

我认为你必须自己动手:

return (x instanceof Foo) ? (Foo) x : null;

编辑:如果您不希望您的客户端代码处理空值,那么您可以引入 Null对象

interface Foo {
    public void doBar();
}
class NullFoo implements Foo {
    public void doBar() {} // do nothing
}
class FooUtils {
    public static Foo asFoo(Object o) {
        return (o instanceof Foo) ? (Foo) o : new NullFoo();
    }
}
class Client {
    public void process() {
        Object o = ...;
        Foo foo = FooUtils.asFoo(o);
        foo.doBar(); // don't need to check for null in client
    }
}

您可以使用 instanceof 关键字代替C#的 ,但 没有任何内容。

示例:

if(myThing instanceof Foo) {
   Foo myFoo = (Foo)myThing; //Never throws ClassCastException
   ...
}

您可以编写这样的静态实用程序方法。我不认为它非常易读,但它是你想要做的最好的近似。如果你使用静态导入,那么在可读性方面也不会太糟糕。

package com.stackoverflow.examples;
public class Utils {
    @SuppressWarnings("unchecked")
    public static <T> T safeCast(Object obj, Class<T> type) {
        if (type.isInstance(obj)) {
            return (T) obj;
        }
        return null;
    }
}

这是一个测试案例,演示了它是如何工作的(并确实有效。)

package com.stackoverflow.examples;
import static com.stackoverflow.examples.Utils.safeCast;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertNull;

import org.junit.Test;

public class UtilsTest {

    @Test
    public void happyPath() {
        Object x = "abc";
        String y = safeCast(x, String.class);
        assertNotNull(y);
    }

    @Test
    public void castToSubclassShouldFail() {
        Object x = new Object();
        String y = safeCast(x, String.class);
        assertNull(y);
    }

    @Test
    public void castToUnrelatedTypeShouldFail() {
        Object x = "abc";
        Integer y = safeCast(x, Integer.class);
        assertNull(y);
    }
}

在java 8中,您还可以使用带有Optional:

的流语法
Object o = new Integer(1);

Optional.ofNullable(o)
        .filter(Number.class::isInstance)
        .map(Number.class::cast)
        .ifPresent(n -> System.out.print("o is a number"));

我猜测你可以提供一个as as operator

类似

as<T,Type> (left, right)  
which evaluates to 
if (typeof(left) == right)
   return (right)left
else
    return null

我不确定你是怎么做的,我现在是一个c#,自从我离开大学以来,我的Java帽子变得有点尘土飞扬。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top