English 中文(简体)
核实多处原始类型(例如:转换案件)的最佳方式是什么?
原标题: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 :)

@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);

通知@Override annotation:这是采用接口方法。 这种方法将接受不同的执行类型,因此我不想采用不同的参数类型来制造三种不同的方法。

举例来说,这只是一个案文箱,只接受数字和任何其他东西,因此,它只能由长处和直线的“固定表述”(由定期的<<条码>[0-9]*验证)。

我也喜欢——或许是,最终——接受更像日本国际协力事业团这样的习俗(和简单),但如果这种特殊性使一切变得复杂的话,我会想到其他事情,那不会对此感到太多的担忧。

正如我说过的那样,这一接口的不同实施可能完全不同。

∗∗∗ 显然,我并没有要求转手案件Integers、Longs和Sting(但不能开关)。 直到 Java7(即)为止,我想转而谈一下:。

研究了

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.

<>strong>EDIT: 使用原始类型和自动箱

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

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

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

产出<代码>true,因为末,my Int instance of Integer iskinda real.!

最佳回答

你们可以做的一件事是把你的首选放在一个接口中,这样,任何实施这一接口的价值类型都可以作为价值。

public interface ValueInterface {
    processValue();
}

Then the set value method would look like:

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

现在你们把工作交给知道如何采取行动的实际对象。

问题回答

Another way to handle this is to write code for any type of 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.

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.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签