方法1:A a=new test().new A(); 內部類對象通過外部類的實例對象調用其內部類構造方法產生,如下:
1 public class test{
2 class A{
3 void fA(){
4 System.out.println("we are students");
5 }
6 }
7 public static void main(String args[]){
8 System.out.println("Hello, 歡迎學習JAVA");
9 A a=new test().new A(); //使用內部類
10 a.fA();
11 }
12 }
方法2: fA()方法設為靜態方法。 當主類加載到內存,fA()分配了入口地址,如下:
public class test{
static void fA(){
System.out.println("we are students");
}
public static void main(String args[]){
System.out.println("Hello, 歡迎學習JAVA");
fA(); //使用靜態方法
}
}
方法3: class A與 主類並列,如下:
public class test{
public static void main(String args[]){
System.out.println("Hello, 歡迎學習JAVA");
A a=new A(); //使用外部類
a.fA();
}
}
class A{
void fA(){
System.out.println("we are students");
}
}