如何打印包含对象的变量名称? [英] How do I print the variable name holding an object?

查看:184
本文介绍了如何打印包含对象的变量名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何打印保存我的对象的变量名?

How do I print the variable name holding my object?

例如,我有:

myclass ob=new myclass()

我将如何打印 ob?

推荐答案

对象没有拥有名称,除非你碰巧使用了一个类允许给每个对象一个(例如通过用 getName()检索的变量)。

Objects don't have names, unless you happen to be using a class which allows each object to be given one (e.g. via a variable retrieved with getName()).

特别是,用于引用对象的任何特定变量的名称对于对象本身是完全未知的。所以你做不到:

In particular, the name of any particular variable used to refer to an object is completely unknown to the object itself. So you can't do:

Object foo = new Object();
// There's no support for this
String name = foo.getName(); // expecting to get "foo"

(请记住,几个变量都可以指同一个变量对象,并且不必是引用对象的任何命名变量。)

(Bear in mind that several variables could all refer to the same object, and there don't have to be any named variables referring to an object.)

这篇关于如何打印包含对象的变量名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆