public class MainClass {
public MainClass() {
}
public void f() {
System.out.println("f()");
}
public static class Tester {
public static void main(String[] args) {
MainClass t = new MainClass();
t.f();
}
}
}
With concrete or abstract classes, inner classes are the only
way to produce the effect of multiple implementation inheritance.
class D {
}
abstract class E {
}
class Z extends D {
E makeE() {
return new E() {
};
}
}
public class MainClass {
static void takesD(D d) {
}
static void takesE(E e) {
}
public static void main(String[] args) {
Z z = new Z();
takesD(z);
takesE(z.makeE());
}
}
Local inner class can have a constructor |
interface Counter {
int next();
}
public class MainClass{
private int count = 0;
Counter getCounter(final String name) {
// A local inner class:
class LocalCounter implements Counter {
public LocalCounter() {
// Local inner class can have a constructor
System.out.println("LocalCounter()");
}
public int next() {
System.out.print(name); // Access local final
return count++;
}
}
return new LocalCounter();
}
public static void main(String[] args) {
MainClass lic = new MainClass();
Counter c1 = lic.getCounter("Local inner ");
}
}
http://www.java2s.com/Tutorial/Java/0100__
Class-Definition/Catalog0100__Class-Definition.htm
No comments:
Post a Comment