English 中文(简体)
界定 Java的种类
原标题:Defining a type cast in Java

我想界定 Java中一种任意类型的原始数据类型。 是否可能从一种任意的类型到另一种任意的类型来界定一种投放物?

public class Foo{
    //methods, constructor etc for this class
    ...
    //make it possible to cast an object of type Foo to an integer 
}
//example of how an object of type foo would be cast to an integer
public class Bar(){
    public static void main(String[] args){
        Foo foo1 = new Foo();
        int int1 = (int)foo1;
        System.out.println(int1+"");
    }
}
最佳回答

您可以提出,但你可以提供转换功能:

public class Foo{
    //methods, constructor etc for this class
    ...
    public int toInt(){
        //convert to an int.
    }    
}

律师后来成为:

public class Bar(){
    public static void main(String[] args){
        Foo foo1 = new Foo();
        int int1 = foo1.toInt();
        System.out.println(int1+"");
    }
}
问题回答

不可能从一类类(例如Foo)直接转换成一种原始类型。 相反,你应界定将oo物体的价值作为ger酸(>)回归的方法。

无。 这并不真正有意义:如何使用<代码>。 Foo成为int?

Rather define a sequence of appropriate methods, such as:

public int toInt() { return 42; }

Typecasting between primitive types and Java types, such as between int and Integer, was not even originally part of Java.

Foo必须延长人数。 为什么你们想要这样做? 因此,不能仅仅获得禁忌内的一个隐形变量。





相关问题
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 ...

热门标签