在类内部创建一个公共方法来获取该类实例的变量名

发布于 2025-01-15 07:59:23 字数 1094 浏览 0 评论 0原文

有没有办法在类内部创建一个公共方法,该方法可以返回用于在该类外部声明该类实例的变量名?

一个非常类似的问题是10年前发布的,答案是没有这样的方法。我想知道随着时间的推移是否已经开发出了一种可能的方法。

这是类的一个示例:

import java.lang.reflect.Field;
import java.util.ArrayList;

public class Class {

    private ArrayList<Array> elements; //Array is some other class

    public Class() {
        elements = new ArrayList<>();
    }

    public String getInstanceName() {

        Field[] fields = this.getClass().getDeclaredFields();
        return fields[0].getName(); // returns "elements"
    } 

因此,在类之外,我将声明类的实例并将其视为 HashMap 中的值

public class SomeOtherClass {
   private HashMap<String, Class> classes
      classes = new HashMap<>();
      Class abc = new Class();
      String someKey = "Abc";
      classes.put(someKey, abc);
   

,下面的语句最好返回字符串“abc”,

System.out.println(classes.get(someKey).getName());

但是 print 语句将返回字符串“elements” ”

Is there a way to create a public method inside a class that can return the variable name used to declare an instance of that class outside that class?

A very similar question has been posted 10 years ago and the answer was that there is no such way. I am wondering if there is a possible way that has been developed over the time.

Here is an example of the Class:

import java.lang.reflect.Field;
import java.util.ArrayList;

public class Class {

    private ArrayList<Array> elements; //Array is some other class

    public Class() {
        elements = new ArrayList<>();
    }

    public String getInstanceName() {

        Field[] fields = this.getClass().getDeclaredFields();
        return fields[0].getName(); // returns "elements"
    } 

So, outside the class I would declare the instance of Class and treat it as value in a HashMap

public class SomeOtherClass {
   private HashMap<String, Class> classes
      classes = new HashMap<>();
      Class abc = new Class();
      String someKey = "Abc";
      classes.put(someKey, abc);
   

and the statement below would desirably return the string 'abc'

System.out.println(classes.get(someKey).getName());

However the print statement would return the String "elements"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苍白女子 2025-01-22 07:59:23

不完全是一个答案,但无论如何......

即使有可能,你也不应该这样做。实例的行为不应该取决于它的使用方式。保存实例的变量名称是实例用户的属性,而不是实例的属性。顺便问一下,如果实例被两个变量引用,您会返回哪个名称?

然后,变量命名不应影响程序的行为。您应该始终保留重构的可能性(您的 IDE 肯定有“重命名变量”重构)。让程序对变量名称进行推理会破坏这种可能性,并使其在使用此(否则完全安全)重构时默默地失败。

如果在变量 a 中您需要一个与变量 b 中的实例行为不同的实例,请在构造实例时提供一些区分信息。

最好问问自己,为什么要向实例询问不属于其自身、而是属于其环境的信息(您不会问一个人,某个本地管理部门将其文件保存在哪个抽屉中 - 如果重要的话,您可以询问管理部门)。

设计软件架构时应确保实例的行为仅取决于其内容。如果您需要信息,请询问自然拥有该信息的实例。其他一切只会让你头疼。

Not exactly an answer, but anyway...

Even if it were possible, you SHOULD NOT DO IT. The behaviour of an instance should not depend on how it is used. The variable name holding the instance is a property of the instance's user and not the instance. And by the way, which name would you return if the instance is referenced by two variables?

And then, variable naming should not affect a program's behaviour. You should always reserve the possibility of refactoring (your IDE surely has a "Rename variable" refactoring). Having your program reason about variable names breaks that possibility and makes it silently fail when using this (otherwise completely safe) refactoring.

If, in a variable a you need an instance that behaves differently from one in variable b, supply some distiguishing information when constructing the instances.

Better ask yourself why you want to ask the instance about information not belonging to itself, but to its environment (you wouldn't ask a person in which drawer some local administration keeps his files - if important, you'd ask the administration).

Design your software architecture in such a way that the behaviour of an instance only depends on its contents. If you need an information, ask the instance that naturally owns this information. Everything else will only give you headaches.

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