![](/img/trans.png)
[英]How come the .getClass().getName() method works with type int?
[英]Can I change the behavior of getClass().getName() method
我有一个Foo。
Foo toy=new Foo("toy");
Foo shoe=new Foo("shoes");
我们可以做点什么
1 toy.getClass().getName()
将输出: toy
。
2 shoe.getClass().getName()
将显示: shoe
。
不,不容易。 Class#getName()
用于反射和调试,并且是Java运行时的一部分。 由于java.lang.Class
是最终的,因此您将无法覆盖它( getClass()
,因为getClass()
始终返回Class
,并且与子类没有任何关系,即使您可以做到这一点发生)
相反,只需使用toy.toString()
并将toString方法添加到Foo
类,该方法将输出传递给构造函数的任何值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.