23-方法的重载







方法的重载

  • 重载就是在一个类中,有相同的函数名称,但形参不同的函数

    例如

    public class Demo_02 {
        public static void main(String[] args) {
            int max = max (10,10);
            System.out.println(max);
        }
        public static int max(int a,int b) {
            int result = 0;
            if (a > b) {
                result = a;
            }else if (a < b) {
                result = b;
            }else {
                System.out.println("a==b");
                return 0;
            }
            return result;
        }
    }

    改为

    public class Demo_02 {
        public static void main(String[] args) {
            double max = max (10.0,10.0);
            System.out.println(max);
        }
        public static double max(double a,double b) {
            double result = 0;
            if (a > b) {
                result = a;
            }else if (a < b) {
                result = b;
            }else {
                System.out.println("a==b");
                return 0;
            }
            return result;
        }
    }

    以上两个示例,方法名字相同,但参数类型不同,所以就是方法重载

  • 方法的重载的规则

    • 方法名称必须相同

    • 参数列表必须不同(个数不同、或类型不同、参数排列顺序不同等)

    • 方法 的返回类型可以相同也可以不相同

    • 仅仅返回类型不同不足以成为方法的重载

  • 方法名称相同时,编译器会根据调用方法的参数个数、参数类型等去逐个匹配,以选择对应的方法,如果匹配失败,则编译器报错。


暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇