Questions
---------
1. The program Problem.java doesn't compile. What do you need to do
to make it compile? Why?
[.../tutorial/java/javaOO/QandE-1dot1/Problem.java]
ANSWER: Add "final" in front of the declaration of the <code>timer</code>
variable. As it says in "Other Facts about Nested Classes",
"A nested class declared within a method...has access to any
final, local variables in scope."
2. Use the 1.3 API documentation for the Box class (in the
javax.swing package) to help you answer the following questions.
a. What static nested class does Box define?
ANSWER: Box.Filler
b. What inner class does Box define?
ANSWER: Box.AccessibleBox
c. What is the superclass of Box's inner class?
ANSWER: [java.awt.]Container.AccessibleAWTContainer
d. Which of Box's nested classes can you use from any class?
ANSWER: Box.Filler
e. How do you create an instance of Box's Filler class?
ANSWER:
new Box.Filler(minDimension, prefDimension, maxDimension)
3. What kind of nested class is most appropriate for each of the following
situations? [PENDING: can we make that clearer? Is there a term
for this kind of one-to-one matching question?]
a. The only users of this nested class will be instances of
the enclosing class or instances of the enclosing class's
subclasses.
ANSWER: B
b. Anyone can use this nested class.
ANSWER: C
c. Only instances of the declaring class need to use this nested
class, and a particular instance might use it several times.
ANSWER: F
d. This tiny nested class is used just once, to create an
object that implements an interface.
ANSWER: A
e. This nested class has information about its enclosing class
(not about instances of the enclosing class) and is used
only by its enclosing class and perhaps their subclasses.
ANSWER: D
f. Like e, but not intended to be used by subclasses.
ANSWER: E
A. anonymous inner class
B. protected inner class
C. public static nested class
D. protected static nested class
E. private static nested class
F. private inner class
Exercises
---------
1. First, get the source file InnerClassDemo.java.
[.../tutorial/java/javaOO/QandE-1dot2/InnerClassDemo.java]
a. Compile and run InnerClassDemo. If you have problems, see
Compiling and Running Swing Programs
[http://docs.oracle.com/javase/javatutorials/tutorialJWS/uiswing/start/swingStart.html].
ANSWER: N/A. [This step is here just to make sure they can compile
and run Swing programs, before they start trying to change
the programs.]
b. Make a copy of InnerClassDemo. Add to it an inner class named
MyActionListener that implements the ActionListener interface
[http://java.sun.com/products/jdk/1.3/docs/api/java/awt/event/ActionListener.html].
The ActionListener interface defines a single method. Put the
following code into your implementation of the method:
quit();
Delete the "//" in front of the following line of code:
//button.addActionListener(new MyActionListener());
Now compile and run the program. What is the difference in
behavior between this version and the previous version of
InnerClassDemo?
ANSWER: See InnerClassDemo2.java
[.../tutorial/java/javaOO/QandE-1dot2/InnerClassDemo2.java]
In this version, the button actually *does* something
(makes the program exit).
c. Make a copy of the program you created for b. Change your
ActionListener implementation to be an anonymous inner class.
Hint: The program already has another anonymous inner class
(a WindowAdapter), which you can look at for syntax help.
ANSWER: See InnerClassDemo3.java
[.../tutorial/java/javaOO/QandE-1dot2/InnerClassDemo3.java]
2. Get the file Class1.java
[.../tutorial/java/javaOO/QandE-1dot1/Class1.java].
a. Compile and run Class1. What is the output?
ANSWER:
InnerClass1: getString invoked.
InnerClass1: getAnotherString invoked.
b. Create a file called Class2.java that defines subclasses
of both Class1 and its inner class, InnerClass1. (Call the
subclasses Class2 and InnerClass2, respectively.) InnerClass2
should override the getAnotherString method to return
"InnerClass2 version of getAnotherString invoked".
Class2 should define one constructor and one method:
- A no-argument constructor that initializes the inherited
<code>ic</code>
instance variable to be an instance of InnerClass2.
- A <code>main</code> method that creates an instance
of Class2 and invokes displayStrings on that instance.
What is the output when you run Class2?
ANSWER:
InnerClass1: getString invoked.
InnerClass2 version of getAnother String invoked.
See Class2.java
[.../tutorial/java/javaOO/QandE-1dot1/Class2.java]
没有合适的资源?快使用搜索试试~ 我知道了~
12---Learning-Java.zip_it
共1822个文件
gif:744个
png:595个
java:149个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 162 浏览量
2022-09-24
06:52:19
上传
评论
收藏 16.56MB ZIP 举报
温馨提示
it is about how to learn java
资源推荐
资源详情
资源评论
收起资源包目录
12---Learning-Java.zip_it (1822个子文件)
TOC.auto 11KB
MultiplicationTable.class 3KB
Card.class 2KB
Card.class 2KB
Card2.class 2KB
Card2.class 2KB
Planet.class 2KB
CharSequenceDemo.class 2KB
CharSequenceDemo.class 2KB
CountVowels.class 2KB
Anagram.class 2KB
Anagram.class 2KB
MaxVariablesDemo.class 2KB
StackOfInts.class 2KB
BoxDemo.class 2KB
Palindrome.class 1KB
StringSwitchDemo.class 1KB
Alpha.class 1KB
CharacterDemo.class 1KB
TestFormat.class 1KB
Stack2.class 1KB
Rank.class 1KB
NumberDemo.class 1KB
Unboxing.class 1KB
TrigonometricDemo.class 1KB
ArrayDemo.class 1KB
ArrayOfArraysDemo.class 1KB
DataStructure.class 1KB
SwitchDemoFallThrough.class 1KB
BasicMathDemo.class 1KB
MinVariablesDemo.class 1KB
Bicycle.class 1KB
EnumTest.class 1KB
Cat.class 1KB
ValueOfDemo.class 1KB
ComputeInitials.class 1KB
InstanceofDemo.class 1KB
ComputeResult.class 1KB
Bicycle.class 1KB
BreakWithLabelDemo.class 1017B
AClass.class 1013B
RectanglePlus.class 1002B
Day.class 1001B
DecimalFormatDemo.class 980B
FillerTest.class 971B
ExponentialDemo.class 960B
ContinueDemo.class 927B
BreakDemo.class 914B
Class1.class 911B
ToStringDemo.class 901B
DeltaOne.class 893B
SwitchDemo.class 891B
RoadBike.class 879B
MountainBike.class 878B
SwitchDemo2.class 870B
ArrayOfArraysDemo2.class 861B
Suit.class 859B
FilenameDemo.class 844B
ContinueWithLabelDemo.class 843B
RegionMatchesDemo.class 832B
Cats.class 831B
StackOfInts$StepThrough.class 824B
Class1.class 815B
FPAdder.class 811B
Stack.class 810B
Rectangle.class 810B
Rectangle.class 810B
EnhancedForDemo.class 808B
DisplayDeck2.class 791B
AlphaTwo.class 790B
DisplayDeck.class 786B
DisplayDeck.class 786B
AbstractClassTest.class 779B
Rectangle.class 775B
MultiDimArrayDemo.class 775B
Filename.class 772B
Animal.class 764B
IfElseDemo.class 754B
StackTest2.class 752B
DataStructure$InnerEvenIterator.class 749B
StringDemo.class 738B
ArrayDemo.class 727B
MinDemo.class 718B
ThisHappens.class 712B
Card3.class 701B
StackTest.class 698B
Adder.class 694B
WhatHappens.class 693B
WhileDemo.class 680B
DoWhileDemo.class 680B
ForDemo.class 674B
ArrayOfStringsDemo.class 674B
ComparisonDemo.class 662B
DeltaTwo.class 655B
Deck2.class 635B
ConcatDemo.class 631B
Deck.class 630B
ArrayCopyDemo.class 630B
ArrayCopyDemo.class 630B
Deck.class 630B
共 1822 条
- 1
- 2
- 3
- 4
- 5
- 6
- 19
资源评论
alvarocfc
- 粉丝: 112
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功