zoukankan      html  css  js  c++  java
  • 方法的重载

    概念

    指的是同一个类中定义的多个方法之间的关系

    1.多个方法在同一个类中

    2.多个方法具有相同的方法名

    3.多个方法的参数列表不同-参数的类型,数量,顺序

    重载方法的调用

    当一个重载的方法被调用时,Java 传递的参数列表来表明实际调用的重载方法是哪一个。

    重载方法的好处

    1.调用过程中,减少列表长度

    2.是对原有方法的一种升级,所以方法名相同可以方便原有使用

    3.方便于阅读,优化了程序设计。

    public class TestOverload{
        public static void main(String[] args) {
            //设计重载的方法比较两个整数是否相同包含来兴byte,short,int,long
            compare((byte)1,(byte)1);
            compare((short)1,(short)1);
            compare((int)1,(int)1);
            compare((long)1,(long)1);
        }
        public static boolean compare(byte b1,byte b2){
            System.out.println("两个byte类型的比较");
            return b1==b2;
        }
        public static boolean compare(short s1,short s2){
            System.out.println("两个short类型的比较");
            return s1==s2;
        }
        public static boolean compare(int i1,int i2){
            System.out.println("两个int类型的比较");
            return i1==i2;
        }
        public static boolean compare(long l1,long l2){
            System.out.println("两个long类型的比较");
            return l1==l2;
        }
    }
  • 相关阅读:
    并查集
    博弈——Bash、Nim、Wythoff
    博弈——SG函数模板
    数据结构——二叉树
    数据结构——链式队列
    sonar-maven-plugin问题
    确立核心模型
    调度思路+EurekaServer获得当前机器的instanceid
    简易的RestClient代码
    Spring Cloud App(Service) Pom示例
  • 原文地址:https://www.cnblogs.com/erlangha/p/12630430.html
Copyright © 2011-2022 走看看