![](/img/trans.png)
[英]How to get the equivalement primitive types from an object of java.lang type in Java?
[英]how to get primitive type from object in Java?
令 x 为原始类型的给定 object,例如 x 由int x;
或char x;
或....等等。
那么,如何从x
获取 class 的名称(即int
或char
或...等)?
动机:
if(primitive type of x = int) then ....
if(primitive type of x = char) then ....
您所描述的是原始类型,而不是类。 你可以这样做。
class Foo {
public int x;
public char y;
}
Foo foo = new Foo();
Field[] fields = foo.getClass().getDeclaredFields();
for (Field f : fields) {
System.out.println(f.getAnnotatedType() + " " + f.getName());
}
印刷
int x
char y
如果您只想让 getter 返回原始字段的值,您可以在 class 中执行此操作。
int someVal = 10;
public int getSomeVal() {
return someVal;
}
这应该可以帮助您开始学习 Java 课程。
public int x
class DoSomething (int x) {
...
}
一些相关链接:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.