• 方法的重载


    所谓的重载就是方法名称相同,但参数的类型和个数不同

    例如:

    public class MethoDemo03{
    public static void main(String args[]){
    int one = add(10,20) ; // 调用整型的加法操作
    float two = add(10.3f,13.3f) ; // 调用浮点数的加法操作
    int three = add(10,20,30) ; // 调用有三个参数的加法操作
    System.out.println("add(int x,int y)的计算结果:" + one) ;
    System.out.println("add(float x,float y)的计算结果:" + two) ;
    System.out.println("(int x,int y,int z)的计算结果:" + three) ;
    }
    // 定义方法,完成两个数字的相加操作,方法返回一个int型数据
    public static int add(int x,int y){
    int temp = 0 ; // 方法中的参数,是局部变量
    temp = x + y ; // 执行加法计算
    return temp ; // 返回计算结果
    }
    public static int add(int x,int y,int z){
    int temp = 0 ; // 方法中的参数,是局部变量
    temp = x + y + z ; // 执行加法计算
    return temp ; // 返回计算结果
    }
    // 定义方法,完成两个数字的相加操作,方法的返回值是一个float型数据
    public static float add(float x,float y){
    float temp = 0 ; // 方法中的参数,是局部变量
    temp = x + y ; // 执行加法操作
    return temp ; // 返回计算结果
    }
    };

    如下例子,参数的类型和个数都相同,只是返回值类型不同,这样的不是重载

    public static int add(int x,int y){
    int temp = 0 ; 
    temp = x + y ; 
    return temp ; 
    }

    public static float add(int x,int y){
    int temp = 0 ;
    temp = x + y ; 
    return temp ;
    }

  • 相关阅读:
    POJ 3268 Silver Cow Party (Dijkstra)
    怒学三算法 POJ 2387 Til the Cows Come Home (Bellman_Ford || Dijkstra || SPFA)
    CF Amr and Music (贪心)
    CF Amr and Pins (数学)
    POJ 3253 Fence Repair (贪心)
    POJ 3069 Saruman's Army(贪心)
    POJ 3617 Best Cow Line (贪心)
    CF Anya and Ghosts (贪心)
    CF Fox And Names (拓扑排序)
    mysql8.0的新特性
  • 原文地址:https://www.cnblogs.com/fenr9/p/5676283.html
Copyright © 2020-2023  润新知