Bring you in-depth understanding of the supplier java1.8

supplier is also used to create the object, but unlike the conventional object syntax to create: new, see the code below:
public class TestSupplier {
Private int Age;
(www.0831jlyy.com)
TestSupplier () {
System.out.println (Age );
}
public static void main (String [] args) {
// create Supplier container TestSupplier declared type, and at this time does not call the constructor of the object, i.e., does not create an object
Supplier <TestSupplier> sup = TestSupplier :: new new;
System.out.println ( "--------");
// call the get () method, then call the object's constructor will, that is, to get the true object of
sup.get ();
// each method call the constructor will get, i.e. a different object acquired
sup.get ();
}
}
the output:

--------
0
0
官方代码及注释:
/**(m.jlnk3659999.com)
* Represents a supplier of results.
*
* <p>There is no requirement that a new or distinct result be returned each
* time the supplier is invoked.
*
* <p>This is a <a href="package-summary.html" rel="external nofollow" >functional interface</a>
* whose functional method is {@link #get()}.
*
* @param <T> the type of results supplied by this supplier
*
* @since 1.8
*/
@FunctionalInterface
public interface Supplier<T> {
(3g.xcjl0834.com)
/**
* Gets a result.
*
* @return a result
*/
GET T ();
}
According to the code and the official comments, my personal understanding:
1.supplier is the interface, there is a get () method
2. Syntax:
Supplier <TestSupplier> SUP = TestSupplier :: new new;
3. GET each call () will call the constructor method to create a new object.

Guess you like

Origin www.cnblogs.com/HanaKana/p/12038204.html