Movatterモバイル変換


[0]ホーム

URL:


Documentation

The Java™ Tutorials
Classes and Objects
Classes
Declaring Classes
Declaring Member Variables
Defining Methods
Providing Constructors for Your Classes
Passing Information to a Method or a Constructor
Objects
Creating Objects
Using Objects
More on Classes
Returning a Value from a Method
Using the this Keyword
Controlling Access to Members of a Class
Understanding Class Members
Initializing Fields
Summary of Creating and Using Classes and Objects
Questions and Exercises
Questions and Exercises
Nested Classes
Inner Class Example
Local Classes
Anonymous Classes
Lambda Expressions
Method References
When to Use Nested Classes, Local Classes, Anonymous Classes, and Lambda Expressions
Questions and Exercises
Enum Types
Questions and Exercises
Trail: Learning the Java Language
Lesson: Classes and Objects
Home Page >Learning the Java Language >Classes and Objects
« Previous • Trail • Next »

The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.
SeeDev.java for updated tutorials taking advantage of the latest releases.
SeeJava Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.
SeeJDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.

Questions and Exercises: Nested Classes

Questions

  1. The programProblem.java doesn't compile. What do you need to do to make it compile? Why?

  2. Use the Java API documentation for theBox class (in thejavax.swing package) to help you answer the following questions.

    1. What static nested class doesBox define?

    2. What inner class doesBox define?

    3. What is the superclass ofBox's inner class?

    4. Which ofBox's nested classes can you use from any class?

    5. How do you create an instance ofBox'sFiller class?

Exercises

  1. Get the fileClass1.java. Compile and runClass1. What is the output?

  2. The following exercises involve modifying the classDataStructure.java, which the sectionInner Class Example discusses.

    1. Define a method namedprint(DataStructureIterator iterator). Invoke this method with an instance of the classEvenIterator so that it performs the same function as the methodprintEven.

    2. Invoke the methodprint(DataStructureIterator iterator) so that it prints elements that have an odd index value. Use an anonymous class as the method's argument instead of an instance of the interfaceDataStructureIterator.

    3. Define a method namedprint(java.util.function.Function<Integer, Boolean> iterator) that performs the same function asprint(DataStructureIterator iterator). Invoke this method with a lambda expression to print elements that have an even index value. Invoke this method again with a lambda expression to print elements that have an odd index value.

    4. Define two methods so that the following two statements print elements that have an even index value and elements that have an odd index value:

      DataStructure ds = new DataStructure()// ...ds.print(DataStructure::isEvenIndex);ds.print(DataStructure::isOddIndex);

Check your answers.

« PreviousTrailNext »

About Oracle |Contact Us |Legal Notices |Terms of Use |Your Privacy Rights

Copyright © 1995, 2024 Oracle and/or its affiliates. All rights reserved.

Previous page: When to Use Nested Classes, Local Classes, Anonymous Classes, and Lambda Expressions
Next page: Enum Types

[8]ページ先頭

©2009-2025 Movatter.jp