Lets assume i have the following class definition:
class Test{
public String toString(){
return "hello test";
}
Now from another class i do the following:
Test myTest=new Test();
//output of below will be 'hello test'
System.out.println(myTest);
I am looking for the equivalent in Android so i could do something like this on objects:
Log.d("TAG",myTest);
or even createToast(Context,myTest,Toast.short).show();
I dont want to have to call the objects toString method, i just want to dump the object into the method and it knows it needs to call toString just like system.out.println did.
since Log.X only takes a String as second parameter, you'd better do a wrapper that calls the Android logger, something like:
static class MyLogger{
public static void d(String tag,Object o){
if(o==null){
throw new NullPointerException("The second parameter can not be null");
}
Log.d(tag, o.toString());
}
}
adding an object to a string will adds its "toString" method without your call to it
Log.d("TAG", "" + myTest);
or even
createToast(Context, "" + myTest,Toast.short).show();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.