System, but they may not be reproduced for publication



Yüklə 83 Mb.
Pdf görüntüsü
səhifə65/82
tarix19.04.2023
ölçüsü83 Mb.
#106251
1   ...   61   62   63   64   65   66   67   68   ...   82
Java A Beginner’s Guide, Eighth Edition ( PDFDrive )

reference. A method reference provides a way to refer to a method without executing it.
It relates to lambda expressions because it, too, requires a target type context that
consists of a compatible functional interface. When evaluated, a method reference also
creates an instance of a functional interface. There are different types of method
references. We will begin with method references to static methods.
Method References to static Methods
A method reference to a static method is created by specifying the method name
preceded by its class name, using this general syntax:
ClassName::methodName
Notice that the class name is separated from the method name by a double colon. The ::
is a separator that was added to Java by JDK 8 expressly for this purpose. This method
reference can be used anywhere in which it is compatible with its target type.
The following program demonstrates the static method reference. It does so by first
declaring a functional interface called IntPredicate that has a method called test().
This method has an int parameter and returns a boolean result. Thus, it can be used
to test an integer value against some condition. The program then creates a class called
MyIntPredicates, which defines three static methods, with each one checking if a
value satisfies some condition. The methods are called isPrime(), isEven(), and
isPositive(), and each method performs the test indicated by its name. Inside
MethodRefDemo, a method called numTest() is created that has as its first
parameter, a reference to IntPredicate. Its second parameter specifies the integer
being tested. Inside main(), three different tests are performed by calling numTest(),
passing in a method reference to the test to perform.



The output is shown here:
In the program, pay special attention to this line:


Here, a reference to the static method isPrime() is passed as the first argument to
numTest(). This works because isPrime is compatible with the IntPredicate
functional interface. Thus, the expression MyIntPredicates::isPrime evaluates to a
reference to an object in which isPrime() provides the implementation of test() in
IntPredicate. The other two calls to numTest() work in the same way.
Method References to Instance Methods
A reference to an instance method on a specific object is created by this basic syntax:
objRef::methodName
As you can see, the syntax is similar to that used for a static method, except that an
object reference is used instead of a class name. Thus, the method referred to by the
method reference operates relative to objRef. The following program illustrates this
point. It uses the same IntPredicate interface and test() method as the previous
program. However, it creates a class called MyIntNum, which stores an int value and
defines the method isFactor(), which determines if the value passed is a factor of the
value stored by the MyIntNum instance. The main() method then creates two
MyIntNum instances. It then calls numTest(), passing in a method reference to the
isFactor() method and the value to be checked. In each case, the method reference
operates relative to the specific object.


This program produces the following output:
In the program, pay special attention to the line
Here, the method reference assigned to ip refers to an instance method isFactor() on


myNum. Thus, when test() is called through that reference, as shown here:
the method will call isFactor() on myNum, which is the object specified when the
method reference was created. The same situation occurs with the method reference
myNum2::isFactor, except that isFactor() will be called on myNum2. This is
confirmed by the output.
It is also possible to handle a situation in which you want to specify an instance method
that can be used with any object of a given class—not just a specified object. In this
case, you will create a method reference as shown here:

Yüklə 83 Mb.

Dostları ilə paylaş:
1   ...   61   62   63   64   65   66   67   68   ...   82




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©genderi.org 2024
rəhbərliyinə müraciət

    Ana səhifə