TAILIEUCHUNG - Programming Groovy dynamic productivity for the java developer phần 4

Hãy nhớ, Java 5 varargs cho phép bạn vượt qua một số biến của các đối số phương pháp, chẳng hạn như phương pháp printf (). Để sử dụng tính năng này trong Java, bạn đánh dấu dấu các loại tham số của một phương pháp với một dấu chấm lửng, như ở nơi công cộng tối đa đối tượng tĩnh (Object. args). Đây là | Closures 94 println Squares of even numbers from 1 to 10 is sqr 10 The code that does the looping is the same and duplicated in each of the previous code examples. What s different is the part dealing with the sum product or squares. If you want to perform some other operation over the even numbers you d be duplicating the code that traverses the numbers. Let s find ways to remove that duplication. The Groovy Way Let s start with a function that allows you to simply pick even numbers. once the function picks a number it immediately sends it to a code block for processing. Let the code block simply print that number for now Download UsingClosures def pickEven n block for int i 2 i n i 2 block i pickEven 10 println it The pickEven 2 method is iterating over values like before but this time it yields or sends the value over to a block of code or closure. The variable block holds a reference to a closure. Much like the way you can pass objects around you can pass closures around. The variable name does not have to be named block it can be any legal variable name. When calling the method pickEven you can now send a code block as shown in the earlier code. The block of code the code within is passed for the parameter block like the value 10 for the variable n. In Groovy you can pass as many closures as you want. so the first third and last arguments for a method call for example may be closures. If a closure is the last argument however there is an elegant syntax as shown here Download UsingClosures pickEven 10 println it 2. pickEven is a higher-order Junction a function that takes functions as arguments or returns a function as a result http cgi wiki7HigherOrderFunction . Closures 95 If the closure is the last argument to a method call you can attach the closure to the method call as shown earlier. The code block in this case appears like a parasite to the method call. unlike Java code blocks Groovy closures can t stand alone they

TỪ KHÓA LIÊN QUAN
TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã 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.