如何将类的字段名称作为参数传递到字符串中?

发布于 2025-02-12 02:58:11 字数 435 浏览 1 评论 0原文

我想知道如何将类的字段名称作为参数传递到字符串中?

例如,假设我与此成员有一个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 技术交流群。

扫码二维码加入Web技术交流群
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文