Java Reference
In-Depth Information
public int getValue() {
return rand.nextInt();
}
}
Listing 2-6. A Local Inner Class That Inherits from Another Class
// RandomLocal.java
package com.jdojo.innerclasses;
public class RandomLocal {
public RandomInteger getRandomInteger() {
// Local inner class that inherits RandomInteger class
class RandomIntegerLocal extends RandomInteger {
@Override
public int getValue() {
// Get two random integers and return the average
// ignoring the fraction part
long n1 = rand.nextInt();
long n2 = rand.nextInt();
int value = (int) ((n1 + n2)/2);
return value;
}
}
return new RandomIntegerLocal();
} // End of getRandomInteger() method
}
Listing 2-7. Testing a Local Inner Class
// LocalInnerTest.java
package com.jdojo.innerclasses;
public class LocalInnerTest {
public static void main(String[] args) {
// Generate random integers using the RandomInteger class
RandomInteger rTop = new RandomInteger();
System.out.println("Random integers using Top-level class:");
System.out.println(rTop.getValue());
System.out.println(rTop.getValue());
System.out.println(rTop.getValue());
// Generate random integers using the RandomIntegerLocal class
RandomLocal local = new RandomLocal();
RandomInteger rLocal = local.getRandomInteger();
 
Search WWH ::




Custom Search