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

71 篇文章 0 订阅

方法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


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值