zoukankan      html  css  js  c++  java
  • 大龄屌丝自学笔记Java零基础到菜鸟012

    方法,重载

    1、方法:完成特定功能的代码块

    格式:修饰符 返回值 方法名(参数类型 参数名1, ...... ,参数类型 参数名n){ 函数体; return 返回值; } 。

    注意:“void” 方法不需要返回值,方法不能嵌套。

     1 class Fin{
     2 
     3     //main方法
     4     public static void main(String[] args){
     5         int n=test(3,2);        //调用test方法
     6         System.out.println(n);  //n=1  
     7     }
     8 
     9     //test方法
    10     public static int test(int a,int b){
    11          return a-b;
    12     }
    13 }

    2、重载:同一个类中,允许同名不同参数(参数类型或参数个数不同)的方法

    注意:与方法的返回值无关,只看方法名和参数(参数类型、参数个数),通过参数区分同名方法。

     1 class Fin{
     2     public static void main(String[] args){
     3         int n=test(3,2,1);
     4         System.out.println(n);  //n=6
     5     }
     6 
     7     public static int test(int a,int b){
     8         return a-b;
     9     }
    10 
    11     public static int test(int a,int b,int c){
    12         return a+b+c;
    13     }
    14 }
  • 相关阅读:
    python-文件操作
    python之-字符编码
    课程总结
    IO流文件输出流的应用
    字符串的基本操作
    数据结构字符串实训报告
    窗口的切换
    事件处理
    Java异常处理
    二维数组实现转置
  • 原文地址:https://www.cnblogs.com/liverpool/p/4761803.html
Copyright © 2011-2022 走看看