加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Java 怎样实现调用其他方法

发布时间:2020-12-15 07:41:21 所属栏目:Java 来源:网络整理
导读:Java主类的main方法调用其他方法 方法1: funA()方法设为静态方法。 当主类加载到内存,funA()分配了入口地址,主要代码如下: public class test{ static void funA(){ System. out .println( " we are students " ); } public static void main(String args[

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();     
        }  
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读