9

sometimes printf("%p", this) helps to see different instances.

What's the equivalent of that in android?
(to print out address of this variable or something unique(it may not be address) to the instance)

It seems I can new interface like OnTouchListener then, how do I print something to differentiate the different instances of them?

4

2 に答える 2

11

このような何かがそれを行う必要があります:

android.util.Log.i("Instance", "This is: " + this);

デフォルトでは、 のtoString実装は、クラス タイプとハッシュ コードを出力します。これは、C++のthis ポインターObjectとある程度同等と見なすことができます。

classのtoStringメソッドは、オブジェクトがインスタンスであるクラスの名前、アットマーク文字、およびオブジェクトのハッシュ コードの符号なし 16 進数表現でObject構成される文字列を返します。'@'つまり、このメソッドは次の値に等しい文字列を返します。

getClass().getName() + '@' + Integer.toHexString(hashCode())

たとえばオブジェクトが の異なる実装を提供する場合、上記の base メソッドの標準的な実装を使用しtoString()て同じ結果を得ることができます。StringtoString()

于 2012-12-27T05:25:00.240 に答える
1

インスタンスとは異なるものを取得するには、 を使用する必要がありますSystem.identityHashCode(this).hashCode()inのデフォルトの実装が返すものObject(サブクラスでオーバーライドされている可能性があるため、.hashCode()直接使用しないでください)を返します。ドキュメントによると、これは「合理的に実用的である限り」個別のオブジェクトに対して異なります。

于 2012-12-27T08:54:30.397 に答える