Groovy学习笔记——扩展toString方法

Groovy学习笔记——扩展toString方法

		class TestClass{
		    String proper
		    void setProper(String proper){
	       	 	this.proper = proper
		    }
		
		    void print() {
	        	println(this.proper)
		    }
		}

        TestClass.metaClass.toString = { ->
            def buffer = new StringBuffer()
            for (def key : TestClass.metaClass.getProperties()) {
                if (key.field != null) {
                    buffer.append("[" + key.field.name + "," +
                            TestClass.metaClass.getProperty(delegate, key.field.name) + "]\n")
                }
            }
            return buffer.toString()
        }

        TestClass clz = new TestClass()
        clz.setProper("TestClass1111")
        log.d(TAG, "TestClass:" + clz.toString())

你可能感兴趣的:(其他)