gpt4 book ai didi

android - 在 android 中打印 `this` 变量

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:02:39 30 4
gpt4 key购买 nike

有时 printf("%p", this) 有助于查看不同的实例。

在android中相当于什么?
(打印出 this 变量的地址或实例的唯一地址(它可能不是 address))

看来我可以像OnTouchListener那样接口(interface),那么,我该如何打印一些东西来区分它们的不同实例呢?

最佳答案

应该这样做:

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

默认情况下,ObjecttoString 实现将打印类类型加上哈希码,这可以被认为在某种程度上等同于this 指针 在 C++ 中。

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character '@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

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

如果对象提供了 toString() 的不同实现,例如 String,那么您可以使用上述基本 toString 的规范实现() 方法得到相同的结果。

关于android - 在 android 中打印 `this` 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14049706/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com