简体   繁体   English

使用原始类型验证多个 instanceof 的最佳方法是什么(例如:switch case)?

[英]What is the best way to verify multiple instanceof's with primitive types (eg: switch case)?

I've searched for answers here and every thread I found were in fact "fragments" of what I seek.我在这里搜索了答案,我发现的每个线程实际上都是我所寻求的“片段”。

I'd like to find a better way than this:我想找到比这更好的方法:

~ EDIT: OOPS, I meant to use primitive Wrapper classes in the first place, but I was thinking of using primitive types when calling the method at that very moment~ Thank you for noticing it:) ~ 编辑:哎呀,我本来打算使用原始的 Wrapper 类,但当时我正在考虑在调用方法时使用原始类型~谢谢你注意到它:)

@Override
public void setValue(Object value) {

    if (value instanceof String) {

    } else if (value instanceof Integer) { // and not 'int'

    } else if (value instanceof Long) { // and not 'long'

    }
}

// The usage that made me confused in the first place :
int i = 42;
setValue(i);

Notice the @Override annotation: this is an implementation of an interface method.注意@Override 注解:这是一个接口方法的实现。 This method would accept different types based on the implementation so I don't want to create three disctinct methods using different parameter types.此方法将根据实现接受不同的类型,因此我不想使用不同的参数类型创建三个不同的方法。

In this example, this is a textbox that only accepts digits and nothing else, so it can only be represented by a String (which is validated by a Regular Expression ^[0-9]*$ ), a long and an int.在此示例中,这是一个仅接受数字且不接受其他任何内容的文本框,因此它只能由字符串(由正则表达式^[0-9]*$验证)、long 和 int 表示。

I'd also like it to - maybe, eventually - accept custom (and simple) DTO's that are more like POJOs, but if this particularity makes everything else complicated I've got something else in mind so don't worry too much about this one.我也希望它 - 也许最终 - 接受更像 POJO 的自定义(和简单)DTO,但如果这种特殊性使其他一切变得复杂,我有别的想法,所以不要太担心这个一。

As I said, different implementations of this interface could accept totally different types.正如我所说,这个接口的不同实现可以接受完全不同的类型。

* I am obviously not asking a way to switchcase Integers, Longs and String (which cannot be switchcased, yet. Not until Java7 that is), I want to switchcase the instanceofs * *我显然不是在问一种方法来切换整数、长整数和字符串(还不能被切换。直到Java7),我想切换instanceofs *

After looking at看了之后

My implementation obviously works, but I just feel there's a better way.我的实现显然有效,但我只是觉得有更好的方法。 I am wondering if there is a cleaner method than doing what I did, what do you suggest and why?我想知道是否有比我做的更清洁的方法,你有什么建议,为什么?

Thank you for your time.感谢您的时间。 Sincerely, Dominic Brissette.此致,多米尼克布里塞特。

EDIT: usage with primitive types and Autoboxing编辑:使用原始类型和自动装箱

public static void main(String[] args) {
    int i = 42;

    System.out.println(autoboxing(i));
}

public static boolean autoboxing(Object o) {
    return o instanceof Integer;
}

Outputs true because in the end, myInt instanceof Integer is kinda true..!输出true ,因为最后, myInt instanceof Integer有点真..!

One thing you could do is wrap your primitive in an interface, and this way any value type implementing this interface could be passed as a value.您可以做的一件事是将您的原语包装在一个接口中,这样实现此接口的任何值类型都可以作为值传递。

public interface ValueInterface {
    processValue();
}

Then the set value method would look like:然后设置值方法如下所示:

public void setValue(ValueInterface value) {
   value.processValue();
}

Now you're delegating the work to the actual object who knows how to act on itself.现在您将工作委派给知道如何自行操作的实际 object。

Another way to handle this is to write code for any type of object.处理此问题的另一种方法是为任何类型的 object 编写代码。

private JLabel label;
@Override
public void setValue(Object value) {
    String text = value.toString();
    Long.parseLong(text); // to validate as a long value.
    label.setText(text);
}

The clean way to switch on type is to use overloading.打开类型的干净方法是使用重载。 Note: An Object cannot be a primitive.注意:Object 不能是原语。

public void setValue(String value) {

}

public void setValue(int value) { // shouldn't need this.

}

public void setValue(long value) {

}

It is highly likely you don't need a seperate int and long method as the later is likely to be enough.您很可能不需要单独的intlong方法,因为后者可能就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM