Java主类的main方法调用其他方法
方法1: funA()方法设为静态方法。 当主类加载到内存,funA()分配了入口地址,主要代码如下:
public class test{ static void funA(){ System.out.println("we are students"); } public static void main(String args[]){ System.out.println("Hello, 欢迎学习JAVA"); funA(); //使用静态方法 } }
方法2: class A与 主类并列,如下
public class testDemo { /* * 成绩统计 * */ public static void main(String[] args) { Integer[] chinaScore = {100,70,80,60,88}; int count=0; //合计值 for(int i = 0;i<chinaScore.length;i++) { count+=chinaScore[i]; } System.out.println("总成绩:"+count); //平均值 int scoreAvg = count/chinaScore.length; System.out.println(scoreAvg); //保留2位小数 DecimalFormat df = new DecimalFormat(".00"); System.out.println(df.format(scoreAvg)); //最大值 int min = (int) Collections.min(Arrays.asList(chinaScore)); int max = (int) Collections.max(Arrays.asList(chinaScore)); System.out.println("历史最高分:" + max); funA();//调用 testClass otherFun = new testClass();//使用外部类 otherFun.vo(); } /* * 自定义funA()函数静态方法 * **/ static void funA() { System.out.println("we are students"); } } //class A与 主类并列,main方法中调用其他类的函数 class testClass{ void vo() { System.out.println("你很帅"); } }
方法3:A a=new test().new A(); 内部类对象通过外部类的实例对象调用其内部类构造方法产生
public class test{ class A{ void fA(){ System.out.println("we are students"); } } public static void main(String args[]){ System.out.println("Hello, 欢迎学习JAVA"); A a=new test().new A(); //使用内部类 a.fA(); } }
原文:https://www.cnblogs.com/lvxisha/p/11561463.html