[英]Is it possible to return the name of an Object in Java?
Suppose you create an object: 假设您创建一个对象:
class newClass{
public static void main(String[] args){
Object o = new Object();
}
}
Is it possible to return the name as a string, "o", or in anyform, of the given object? 是否可以将名称以字符串“ o”或任何形式返回给定对象?
No, this is not possible. 不,这是不可能的。 If you really want this, you are probably using the wrong design. 如果您确实要这样做,则可能使用了错误的设计。 Note that objects do not have a name. 请注意,对象没有名称。 Variables do have a name. 变量确实有一个名称。
否。该对象不知道外界称之为什么,即它有什么参考。
Is it possible to return the name of an Object in Java? 是否可以用Java返回对象的名称?
If you write something like this: 如果您编写如下内容:
String name = obj.toString();
It will return string representation of object but this is not very human-readable. 它将返回对象的字符串表示形式,但这不是人类可读的。 if you want to do it you need to create custom object and override toString() method: 如果要执行此操作,则需要创建自定义对象并覆盖toString()方法:
public class MyObject {
private String name;
public String toString() {
return name;
}
}
Now when you write: 现在,当您编写时:
String name = myObj.toString();
It will return name in human-readable form. 它将以人类可读的形式返回名称。 But this depends of your requirements if you want to create custom objects. 但是,如果要创建自定义对象,这取决于您的要求。 In this way it's very simple and it works well. 这样,它非常简单并且运行良好。
This is not possible. 这是不可能的。
You could use a Map<String, Object>
to store a mapping from object name to object value and then return the key. 您可以使用Map<String, Object>
来存储从对象名称到对象值的映射,然后返回键。
In reality the "name" of your object is a compile time constant. 实际上,对象的“名称”是一个编译时间常数。 Ie it doesn't change once you have compiled the code so 也就是说,一旦您编译了代码,它就不会改变,因此
Object o = new Object();
return "o";
Would literally do what you want. 从字面上会做你想要的。 Which begs the question, why would you want to do that? 哪个提出了问题,您为什么要这样做?
Note: I just read sajmon's answer and he has the more elegant way of doing it to a certain extent 注意:我只是阅读了sajmon的答案,他在某种程度上拥有更优雅的用法
First of all the guys' answers are correct 首先,这些家伙的答案是正确的
but 但
if you really need it 如果您真的需要
I would extend Object class with something like MyObject 我会用MyObject之类的东西来扩展Object类
public class MyObject {
private String objectName;
public MyObject(){
}
public MyObject(String name){
setObjectName(name);
}
public getObjectName(){
return this.objectName;
}
public setObjectName(String name){
this.objectName = name;
}
}
but then each time you create an Object use MyObject instance instead and add your object name as a constructor parameter 但是随后每次创建对象时,请改用MyObject实例,然后将对象名称添加为构造函数参数
MyObject mine = new MyObject("mine");
Having said that, it's still an open question as to why you might need it? 话虽如此,关于您为什么可能需要它仍然是一个悬而未决的问题? :) :)
Since Java supports aliasing this really isn't sensible or possible. 由于Java支持别名,因此这确实不可行。 Aliasing means there's no garuantee that an object is referenced by only one variable. 别名意味着没有保证对象只能由一个变量引用。
scoping just makes the whole problem even harder, since only references in the current scope would be of any use at that point. 范围界定只会使整个问题更加棘手,因为此时只有当前范围内的引用才有用。 and there may be more than one. 可能不止一个。
it would help if you explained what you're trying to achieve with this code. 如果您解释了这段代码要达到的目的,这将有所帮助。
In addition to the previous answers, there may be several different variables, with different identifiers, that all reference the same object. 除了先前的答案之外,可能还有几个具有不同标识符的不同变量,它们均引用同一对象。 A variable may refer to different objects at different times. 变量可以在不同的时间引用不同的对象。
If you need a mapping from an object to a String, I suggest a Map<Object,String>
. 如果需要从对象到字符串的映射,建议使用Map<Object,String>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.