如何动态获取类内部的对象名称? [英] How to get the object name inside the class dynamically?
本文介绍了如何动态获取类内部的对象名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
IAutoTransmission Bentley =新的AutoTransmission(5,80,50);
我正在创建一个名为Bentley的对象.在AutoTransmission类中,我想在调用构造函数时动态获取对象名称.
如何获取对象名称?
在构造函数内部,我想获取对象名称.像这样
console.writeline(您的对象名称为{0}",objname); //必须打印"Bentley"
IAutoTransmission Bentley = new AutoTransmission(5,80,50);
I am creating one object called Bentley. Inside the AutoTransmission class i want to get the object name dynamically when we are calling the constructor.
How we can get the object name ??
Inside the constructor i want to get the object name.like this
console.writeline("YOur object name is {0}",objname); //this has to print "Bentley"
推荐答案
您不能-您所谈论的对象名称"是一个引用对象的变量,类型为IAutoTransmission.实际对象没有人类可读的名称".
考虑一下:如果您编写了这段代码
You can''t - the "object name" that you are talking about is a variable which references an object, of type IAutoTransmission. The actual object does not have a human readable "name".
Think about it: if you wrote this code
IAutoTransmission Bentley = new AutoTransmission(5,80,50);
IAutoTransmission Mine = Bently;
您希望它返回哪个名称?
Which name would you expect it to return?
简短的答案:您无法做到这一点.
但是,请参见此线程 [The Competitors"处网站.
Short answer: you cannot do that.
However, please see this thread[^] at ''The Competitors
'' site.
这篇关于如何动态获取类内部的对象名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文