四
方法內部類只在該方法內部可見
/**
* 內部類實現接口
*
* @author leizhimin
*/
public class Test
public static void main(String[] args) {
Outer outer = new Outer();
Foo f = outer
Bar b = outer
f
b
}
}
class Outer {
public Foo genFoo() {
//方法內的內部類
class FooImpl implements Foo {
public void say() {
System
}
}
return new FooImpl();
}
public Bar genBar() {
Bar b = null;
if (true) {
//任意位置的內部類
class BarImpl implements Bar {
public void readme() {
System
}
}
b = new BarImpl();
}
return b;
}
}
運行結果
say foo!
say bar!
Process finished with exit code
五
匿名類不給出類名
在一些多線程程序中比較常見
/**
* 匿名類
*
* @author leizhimin
*/
public class Test
public Foo f = new Foo() {
public void say() {
System
}
};
public Foo test() {
return new Foo() {
public void say() {
System
}
};
}
public static void main(String[] args) {
Test
t
t
}
}
interface Foo {
void say();
}
運行結果
say foo!
Process finished with exit code
/**
* 普通類的匿名初始化
*
* @author leizhimin
*/
public class Fk {
private String x;
public Fk(String x) {
this
}
@Override
public String toString() {
return
}
}
class Test
public Fk hehe() {
//把後面的一對大括號去掉呢
return new Fk(
};
}
public static void main(String[] args) {
Test
Fk f = t
System
}
}
運行結果
Fk{x=
Process finished with exit code
還有一個不得不提的經典實例
interface Service {
void method
void method
}
interface ServiceFactory {
Service getService();
}
class Implementation
private Implementation
public void method
public void method
public static ServiceFactory factory = new ServiceFactory() {
public Service getService() {
return new Implementation
}
};
}
class Implementation
private Implementation
public void method
public void method
public static ServiceFactory factory = new ServiceFactory() {
public Service getService() {
return new Implementation
}
};
}
public class Factories {
public static void serviceConsumer(ServiceFactory fact) {
Service s = fact
thod
thod
}
public static void main(String[] args) {
serviceConsumer(Implementation
serviceConsumer(Implementation
}
}
這個應用給了我們很多思考
內部類的巧妙使用會讓你的代碼很牛
六
靜態內部類是static class型的內部類
new 外部類名
來創建
/**
* 靜態內部類
*
* @author leizhimin
*/
public class Outer {
public static int i =
protected static class Inner {
int i =
String name;
Inner(String name) {
this
}
void sayHello() {
System
Outer
}
}
public Inner genInner(String name) {
return new Inner(name);
}
}
class Test {
public static void main(String[] args) {
Outer
in
System
Outer
in
System
}
}
運行結果
Hello
Hello
Process finished with exit code
七
接口內部類自動都是public static的
static class Entry implements Map
下面我給個例子
/**
* 接口內部類
*
* @author leizhimin
*/
public interface AInterface {
void readme();
class Inner
public void readme() {
System
}
}
}
class Main {
public static void main(String[] args) {
AInterface
in
}
}
八
所謂內部類嵌套
/**
* 嵌套內部類
*
* @author leizhimin
*/
public class Outer {
private void f
System
}
class A {
private void a() {
f
System
}
class B {
protected void b() {
a();
System
}
}
}
}
class Test{
public static void main(String[] args) {
Outer o = new Outer();
Outer
Outer
b
}
}
運行結果
f
a
b
Process finished with exit code
八
內部類的繼承
/**
* 內部類的繼承
*
* @author leizhimin
*/
public class Outer {
class Inner {
void doSomething() {
System
}
}
class Inner
void doSomething() {
System
}
void readme() {
System
}
}
}
class Test {
public static void main(String[] args) {
Outer outer = new Outer();
Outer
Outer
in
in
in
}
}
運行結果
Inner doing
Inner
HeHe!
Process finished with exit code
總結
內部類是Java中最復雜深奧的概念之一
內部類的設計也許是彌補Java語言本身的先天不足吧
以上的總結完全是建立在實踐基礎上的
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26666.html