如何将类的字段名称作为参数传递到字符串中?
我想知道如何将类的字段名称作为参数传递到字符串中?
例如,假设我与此成员有一个A类A:
public class A {
String name;
String reference;
....
}
我想要的是能够初始化具有参数的字符串 两个Classe字段名称之一,可以动态显示。因此,例如,我想这样做:
System.out.println("the field {nameField} belong to class A.");
在输出中,我会有这样的:
output : the field name belong to class A.
在Java中可以吗?
有什么帮助会不胜感激吗?
问候
YT
I'm wondering How can I pass field name of a class as parameter to include it in a String?
For example, let's say I have a class A with this members :
public class A {
String name;
String reference;
....
}
What I want, is to be able to initialize a string having as parameters
one of the two classe field name to display it dynamically. So for instance, I want to do this :
System.out.println("the field {nameField} belong to class A.");
And in output I would have this :
output : the field name belong to class A.
Is that possible in Java ?
Any help would be much appreciated ?
Regards
YT
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可以通过在Java中使用反射来完成。
https://www.oracle.com/technical-resources/articles/java/java/javareflection.html#: text = text = refleflection%20IS%20IS%20A%20A%20A%20A%20Feature%20IN,20IN,20IN,20MEMEMEMBERS %20和%20Display%20them 。
This can be done by use of reflection in Java.
https://www.oracle.com/technical-resources/articles/java/javareflection.html#:~:text=Reflection%20is%20a%20feature%20in,its%20members%20and%20display%20them.