Java內(nèi)部類的實(shí)現(xiàn)方法有以下幾種:
public class OuterClass {
private int outerVar;
public class InnerClass {
public void innerMethod() {
outerVar = 10; // 訪問外部類的成員變量
outerMethod(); // 訪問外部類的方法
}
}
public void outerMethod() {
// ...
}
}
public class OuterClass {
private static int outerVar;
public static class InnerClass {
public void innerMethod() {
outerVar = 10; // 訪問外部類的靜態(tài)成員變量
outerMethod(); // 訪問外部類的靜態(tài)方法
}
}
public static void outerMethod() {
// ...
}
}
public class OuterClass {
public void outerMethod() {
class InnerClass {
public void innerMethod() {
// ...
}
}
InnerClass inner = new InnerClass();
inner.innerMethod();
}
}
public class OuterClass {
public void outerMethod() {
Runnable runnable = new Runnable() {
@Override
public void run() {
// ...
}
};
Thread thread = new Thread(runnable);
thread.start();
}
}