0

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.

4

2 回答 2

2

因为 Log.X 只接受一个字符串作为第二个参数,你最好做一个调用 Android 记录器的包装器,比如:

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());
        }
    }
于 2013-09-04T12:58:01.850 回答
0

将对象添加到字符串将添加其“toString”方法,而无需您调用它

Log.d("TAG", "" + myTest);

甚至

createToast(Context, "" + myTest,Toast.short).show();
于 2013-09-04T13:02:30.250 回答