Get the instance name of this

后端 未结 4 1734
攒了一身酷
攒了一身酷 2021-01-22 08:50

Is this possible!!?!?!?!?

I\'m trying to make a set of classes that model a number of different types of things. Properties of these things change over time, and I want

4条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-22 09:25

    It's not possible to do that kind of reflection. Variable names could be erased by the compiler depending on debugging options and scope. The name of a variable is an alias of its "address" so can not be reversed (at least in Java and C# AFAIK).

    An object can be pointed from nay variables, so the variable name is irrelevant and the object don't know this kind of stuff:

    Cat tiger = new Cat();
    Cat tiger2 = tiger;
    

    Variable tiger2 points to the Cat "tiger", so 2 variable points same object with different names.

    Use class properties passed in the constructor.

提交回复
热议问题