|NO.Z.00021|——————————|BigDataEnd|——|Java&方法封装.V03|——|Java.v03|point类.v02|重载概念|体现形式|

一、重载的概念和体现形式
### --- 方法重载的概念

~~~     ——>        若方法名称相同,参数列表不同,这样的方法之间构成重载关系(Overload)。
### --- 方法重载的体现形式

~~~     ——>        方法重载的主要形式体现在:参数的个数不同、参数的类型不同、参数的顺序不同,与返回值类型和形参变量名无关,但建议返回值类型最好相同。
~~~     ——>        判断方法能否构成重载的核心:# 调用方法时能否加以区分。
二、编程代码
### --- 编程代码

/*
    编程实现方法重载主要形式的测试
 */
public class OverloadTest {
    
    // 自定义成员方法
    void show() {
        System.out.println("show()");
    }
    void show(int i) { // ok  体现在方法参数的个数不同
        System.out.println("show(int)");
    }
    void show(int i, double d) { // ok  体现在方法参数的个数不同
        System.out.println("show(int, double)");
    }
    void show(int i, int j) { // ok  体现在方法参数的类型不同
        System.out.println("show(int, int)");
    }
    void show(double d, int i) { // ok  体现在方法参数的顺序不同
        System.out.println("show(double, int)");
    }
    /*
    void show(double a, int b) { // error 与参数变量名无关
        System.out.println("show(double, int)");
    }
    */
    /*
    int show(double d, int i) { // error, 与返回值类型无关
        System.out.println("show(double, int)");
    }
    */
    
    public static void main(String[] args) {
        
        // 1.声明OverloadTest类型的引用指向该类型的对象
        OverloadTest ot = new OverloadTest();
        // 2.调用show方法
        ot.show();
        ot.show(66);
        ot.show(66, 3.14);
        ot.show(66, 118);
        ot.show(3.14, 118);
        //ot.show(3.14, 66);
    }
}
三、编译打印
### --- 编译

C:\Users\Administrator\Desktop>javac OverloadTest.java
### --- 打印输出

C:\Users\Administrator\Desktop>java OverloadTest
show()
show(int)
show(int, double)
show(int, int)
show(double, int)

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

 

posted on   yanqi_vip  阅读(3)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示