TAILIEUCHUNG - The definitive guide to grails second edition - phần 10

Groovy không hỗ trợ các đối số thực sự tên là nhưng cho phép các phương pháp để xác định một bản đồ như là duy nhất đối số, do đó bắt chước khả năng này. Sự hạn chế này chủ yếu là mã byte Java chính nó, mà không tên không liên kết (chỉ các loại) với các thông số phương pháp. | 568 APPENDIX THE GROOVY LANGUAGE Note Groovy doesn t support true named arguments but allows the method to specify a map as the only argument hence mimicking this capability. This limitation is mainly down to Java byte code itself which does not associate names only types with method parameters. Groovy ships with a number of builders excuse the pun built-in including but not limited to the following The MarkupBuilder for constructing typically XML markup The DOMBuilder for constructing W3C DOM trees The AntBuilder to provide scripting for Apache Ant The SwingBuilder for constructing Swing GUI interfaces As an example take a look at the usage of the MarkupBuilder shown in Listing A-23 which allows construction of markup documents such as XML or HTML. Listing A-23. MarkupBuilder in Action construct builder that outputs to standard out def mkp new write markup author name Stephen King book title The Shining book title The Stand author name James Patterson book title Along Came a Spider This example demonstrates the construction of a instance using standard out and the usage of closures and named arguments to represent the markup. Listing A-24 shows the result. APPENDIX THE GROOVY LANGUAGE 569 Listing A-24. Result of MarkupBuilder authors author name Stephen King book title The Shining book title The Stand author author name James Patterson book title Along Came a Spider author authors It is interesting at this point to take a closer look at Listing A-23. In this example we passed an anonymous closure to the authors method of the MarkupBuilder instance but consider the possibility of assigning this closure to a variable and then passing as an argument the same closure to different builders one that renders XML and another that outputs the same data as a PDF document or renders it in a GUI. Unlike the XML produced in Listing A-24 the builder code in Listing A-23 is pure Groovy code and can therefore leverage

TỪ KHÓA LIÊN QUAN
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.