简体   繁体   English

是否可以用Java返回对象的名称?

[英]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? 哪个提出了问题,您为什么要这样做?

This is not possible using java reflection. 使用Java反射是不可能的。 However, the information is stored in java class file if compiled with debugging information on. 但是,如果使用调试信息进行编译,则该信息将存储在java类文件中。 You may use bytecode engineering library like ASM to dig that. 您可以使用ASM之类的字节码工程库来进行挖掘。

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>

Jackson (Java):反序列化相同的属性名但返回不同的对象。 其中一个返回 object 和第二个列表<object><div id="text_translate"><p>我有一个这样的 POJO:</p><pre> public class NewClass { String name; @JsonProperty("productType") ProductType productType2005; List&lt;ProductType&gt; productType; }</pre><p> 我想将 json 反序列化为 Pojo。 问题是我的属性名称相同 productType 但我可以期待两种不同的返回类型或数据结构。</p><ol><li> 返回ProductType</li><li> return List&lt;ProductType&gt;因为属性名称相同我如何有效地使用 Jackson 注释来解决它?</li></ol><p> 我使用 rest-assured 进行反序列化,使用 Lombok 进行典型的 getter 和 setter。</p></div></object> - Jackson (Java) : deserialization for the same property name but return different objects. for one it return object and second List<Object>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java中是否可以将枚举名称和值一起返回? - Is it possible in Java to return enum name and values together? 是否有可能在运行时获取对象的声明名称? - Is it possible to get the declaration name of an object at runtime in java? 基于java中的输入字符串名称返回对象 - Return object based on input string name in java Java:当我将其名称作为字符串时,如何返回对象? - Java: How to return object when I have its name as a string? Jackson (Java):反序列化相同的属性名但返回不同的对象。 其中一个返回 object 和第二个列表<object><div id="text_translate"><p>我有一个这样的 POJO:</p><pre> public class NewClass { String name; @JsonProperty("productType") ProductType productType2005; List&lt;ProductType&gt; productType; }</pre><p> 我想将 json 反序列化为 Pojo。 问题是我的属性名称相同 productType 但我可以期待两种不同的返回类型或数据结构。</p><ol><li> 返回ProductType</li><li> return List&lt;ProductType&gt;因为属性名称相同我如何有效地使用 Jackson 注释来解决它?</li></ol><p> 我使用 rest-assured 进行反序列化,使用 Lombok 进行典型的 getter 和 setter。</p></div></object> - Jackson (Java) : deserialization for the same property name but return different objects. for one it return object and second List<Object> Java:是否可以命名集合? - Java:Is it possible to name a collection? Java中的Com对象可能吗? - Com object in Java , possible? 在Java中返回匿名对象 - Return anonymous object in java java-返回对象值 - java - return object value 在Java中返回异常对象 - Return exception object in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM