In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail whether Java needs to introduce closures, and the content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.
First of all, let's understand what a closure is.
Closures are blocks of code that can contain free (unbound) variables; these variables are not defined in this code block or any global context, but in the environment in which the code block is defined. the word "closure" comes from a combination of the block of code to be executed (the relevant variable reference is not released due to the existence of free variables) and the computational environment (scope) that provides binding for free variables.
Maybe the above definition is a little obscure, so let's take a look at the "Python Core programming" explanation of closures.
If in an internal function, a variable in the external scope (but not in the global scope) is referenced, then the internal function is defined as a closure. Variables defined in an external function but referenced or used by an internal function are called free variables.
Here is an example of a closure
Python code
Def counter (start_at = 0): count = [start_at] def incr (): count [0] + = 1 return count [0] return incr def counter (start_at = 0): count = [start_at] def incr (): count [0] + = 1 return count [0] return incr
The count variable is a free variable relative to the function incr (it is in the external scope of the function incr, but not in the global scope), and the internal function incr can reference and use this variable. This example mainly simulates a counter.
Run the following code
Java code
Count = counter (6) print count () print count () count = counter (6) print count () print count ()
Will print out
seven
eight
We find that internal functions (incr) can refer not only to variables defined by themselves, but also to variables defined by external functions (counter). Or the internal function (closure) can remember the state, and it can perform different operations according to the state it remembers. The external function is responsible for initializing the state (the state that the internal function needs to remember).
So why do you need closures? what are the advantages of closures? I think it can remember the state, but the object can also remember the state (through the properties of the object). So what's the difference between closures and objects? I think it's because closures are functions, not objects. We will find that if we express the inner function (closure) of the closure in an object-oriented way, it is like the method of the object and the constructor of the external function object. The constructor is used to initialize the state of the object and the methods of the object can perform different operations depending on the state of the object.
good! Let's create a counter in an object-oriented way (implementing the same function as the previous example, implemented in Java).
Java code
Public class Counter {private int startAt; public Counter () {this (0);} public Counter (int startAt) {this.startAt = startAt;} public int incr () {return + + this.startAt;}} public class Counter {private int startAt; public Counter () {this (0) } public Counter (int startAt) {this.startAt = startAt;} public int incr () {return + + this.startAt;}}
Run the Test class
Java code
Public class Test {public static void main (String [] args) {Counter counter = new Counter (6); System.out.println (counter.incr ()); System.out.println (counter.incr ());}} public class Test {public static void main (String [] args) {Counter counter = new Counter (6); System.out.println (counter.incr ()) System.out.println (counter.incr ());}}
Will print out (the same as the printout in the above example)
So does Java (with objects) still need to introduce closures? I don't think so, because the object can completely simulate the behavior of the closure, and the object is the first-level element of OOP. Closure is a concept in functional programming (FP). The introduction of closure is equivalent to the introduction of FP, which will only destroy the purity and simplicity of Java.
On whether the Java needs to introduce closures to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.