JAVA学习50_java 主类的main方法调用其他方法

来源:互联网 发布:淘宝外贸的衣服能买吗 编辑:程序博客网 时间:2024/06/07 04:05

方法1:A a=new test().new A(); 内部类对象通过外部类的实例对象调用其内部类构造方法产生,如下

[java] view plain copy
  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()分配了入口地址。
[java] view plain copy
  1. public class test{  
  2.      static void fA(){  
  3.          System.out.println("we are students");  
  4.          }  
  5.     public static void main(String args[]){  
  6.         System.out.println("Hello, 欢迎学习JAVA");  
  7.         fA(); //使用静态方法  
  8.         }  
  9. }   
[java] view plain copy
  1. 方法3: class A与 主类并列  
[java] view plain copy
  1. public class test{  
  2.     public static void main(String args[]){  
  3.         System.out.println("Hello, 欢迎学习JAVA");  
  4.         A a=new A();  //使用内部类  
  5.         a.fA();     
  6.         }  
  7. }   
  8. class A{  
  9.       void fA(){  
  10.          System.out.println("we are students");  
  11.          }  
  12.       }  
原文来自:http://blog.csdn.net/huacuo2013/article/details/45248841


原创粉丝点击