System, but they may not be reproduced for publication



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

dependence on C. Implied dependence is also referred to as implied readability.
To create an implied dependence, add the transitive keyword after requires in the
clause that requires the module upon which an implied readability is needed. In the
case of this example, you would change B’s module­info file as shown here:
Here, C is now required as transitive. After making this change, any module that
depends on B will also automatically depend on C. Thus, A would automatically have
access to C.


As a point of interest, because of a special exception in the Java syntax, in a requires
statement, if transitive is immediately followed by a separator (such as a semicolon),
it is interpreted as an identifier (for example, as a module name) rather than a keyword.
Try This 15­1
Experiment with requires transitive
You can experiment with requires transitive by reworking the preceding modular
application example. Here, you will remove the isFactor( ) method from the
SimpleMathFuncs class in the appfuncs.simplefuncs package and put it into a
new class, module, and package. The new class will be called SupportFuncs, the
module will be called appsupport, and the package will be called
appsupport.supportfuncs. The appfuncs module will then add a dependence on
the appsupport module by use of requires transitive. This will enable both the
appfuncs and appstart modules to access it without appstart having to provide its
own requires statement. This works because appstart receives access to it through
an appfuncs requires transitive statement.
1. To begin, create the source directories that support the new appsupport module.
To do so, create appsupport under the appsrc directory. This is the module directory
for the support functions. Under appsupport, create the package directory by adding
the appsupport subdirectory followed by the supportfuncs subdirectory. Thus, the
directory tree for appsupport should now look like this:
2. Add the following module­info.java file to the module source directory for
appsupport, which is appsrc\appsupport:
3. In the appsupport.supportfuncs package directory, add the following file called
SupportFuncs.java:


As you can see, the isFactor( ) method is now in SupportFuncs rather than in
SimpleMathFuncs.
4. Remove isFactor( ) from SimpleMathFuncs. Thus, SimpleMathFuncs.java
will now look like this:


Notice that now the SupportFuncs class is imported, and calls to isFactor( ) are
referred to through the class name SupportFuncs.
5. Change the module­info.java file for appfuncs so that in its requires statement,
appsupport is specified as transitive, as shown here:
6. Because appfuncs requires appsupport as transitive, there is no need for the
module­info .java file for appstart to also require it. Its dependence on
appsupport is implied. Thus, no changes to the module­info.java file for appstart
are required.
7. Update MyModAppDemo.java to reflect these changes. Specifically, it must now
import the SupportFuncs class and specify it when invoking isFactor( ), as shown
here:


8. Recompile the entire program using this multimodule compilation command:
As explained earlier, the multimodule compilation will automatically create the parallel
module subdirectories under the appmodules directory.
9. Run the application as before, using this command:
It will produce the same output as before.
10. As an experiment, remove the transitive specifier from the module­info.java
file for appfuncs and then try recompiling. As you will see, an error will result because
appsupport is no longer accessible by appstart.
11. Here is another experiment. In the module­info file for appsupport, try exporting
the appsupport.supportfuncs package to only appfuncs by use of a qualified
export, as shown here:
Next, try recompiling the program. As you can see, the program will not compile


because now the support function isFactor( ) is not available to the
MyModAppDemo, which is in the appstart module. As explained previously, a
qualified export restricts access to a package to only those modules specified by the to
clause.
USE SERVICES
In programming, it is often useful to separate what must be done from how it is done.
As you learned in 
Chapter 8
, one way this is accomplished in Java is through the use of
interfaces. The interface specifies the what, and the implementing class specifies the

Yüklə 83 Mb.

Dostları ilə paylaş:
1   ...   65   66   67   68   69   70   71   72   ...   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ə