System, but they may not be reproduced for publication



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

ClassName::instanceMethodName
Here, the name of the class is used instead of a specific object, even though an instance
method is specified. With this form, the first parameter of the functional interface
method matches the invoking object and the second parameter matches the parameter
(if any) specified by the instance method. Here is an example. It reworks the previous
example. First, it replaces IntPredicate with the interface MyIntNumPredicate. In
this case, the first parameter to test() is of type MyIntNum. It will be used to receive
the object being operated upon. This allows the program to create a method reference
to the instance method isFactor() that can be used with any MyIntNum object.


Ask the Expert


Q
: How do I specify a method reference to a generic method?
A
: Often, because of type inference, you won’t need to explicitly specify a type
argument to a generic method when obtaining its method reference, but Java does
include a syntax to handle those cases in which you do. For example, assuming the
following:
the following statement is valid:
Here, the type argument for the generic method myGenMeth is explicitly
specified. Notice that the type argument occurs after the ::. This syntax can be
generalized: When a generic method is specified as a method reference, its type
argument comes after the :: and before the method name. In cases in which a
generic class is specified, the type argument follows the class name and precedes
the ::.
The output is shown here:
In the program, pay special attention to this line:
It creates a method reference to the instance method isFactor() that will work with
any object of type MyIntNum. For example, when test() is called through the inp, as
shown here:


it results in a call to myNum.isFactor(3). In other words, myNum becomes the
object on which isFactor(3) is called.
NOTE
A method reference can use the keyword super to refer to a superclass version of a
method. The general forms of the syntax are super::methodName and
typeName.super::methodName. In the second form, typeName must refer to the
enclosing class or a superinterface.
CONSTRUCTOR REFERENCES
Similar to the way that you can create references to methods, you can also create
references to constructors. Here is the general form of the syntax that you will use:
classname::new
This reference can be assigned to any functional interface reference that defines a
method compatible with the constructor. Here is a simple example:


The output is shown here:
In the program, notice that the func() method of MyFunc returns a reference of type
MyClass and has a String parameter. Next, notice that MyClass defines two
constructors. The first specifies a parameter of type String. The second is the default,
parameterless constructor. Now, examine the following line:
Here, the expression MyClass::new creates a constructor reference to a MyClass
constructor. In this case, because MyFunc’s func() method takes a String parameter,
the constructor being referred to is MyClass(String s) because it is the one that
matches. Also notice that the reference to this constructor is assigned to a MyFunc
reference called myClassCons. After this statement executes, myClassCons can be
used to create an instance of MyClass, as this line shows:
In essence, myClassCons has become another way to call MyClass(String s).
If you wanted MyClass::new to use MyClass’s default constructor, then you would
need to use a functional interface that defines a method that has no parameter. For
example, if you define MyFunc2, as shown here:


then the following line will assign to MyClassCons a reference to MyClass’s default
(i.e., parameterless) constructor:
In general, the constructor that will be used when ::new is specified is the one whose
parameters match those specified by the functional interface.
Ask the Expert
Q
: Can I declare a constructor reference that creates an array?
A
: Yes. To create a constructor reference for an array, use this construct:

Yüklə 83 Mb.

Dostları ilə paylaş:
1   ...   62   63   64   65   66   67   68   69   ...   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ə