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

    在java里,一个函数想要传入不同的数据类型的参数,就要写多个函数,每个函数名一样,参数不一样,这一点和python不同,python一个函数就可以支持多种的数据类型。

    方法重载(overload)

    1. 概念:一个类中的一组方法,相同的方法名字,不同的参数列表,这样的一组方法构成了方法重载

      参数列表的不同体现在:参数的个数,参数的类型,参数的顺序

    2. 作用:为了让使用者便于记忆与调用,只需要记录一个名字,执行不同的操作

    3. 自己设计方法重载

      调用方法的时候,首先通过方法名字定位方法

      如果方法名字有一致,可以通过参数的数据类型定位方法

      如果没有与传递参数类型一致的方法,可以找一个参数类型可以进行转化(自动)

      public class TestOverLoad {
          public void test(){
              System.out.println("执行了test方法没有携带参数");
          }
          public void test(boolean b){
              System.out.println("执行了test方法带boolean参数"+b);
          }
          public void test(int i){
              System.out.println("执行了test方法带int参数"+i);
          }
      //    public void test(char c){
      //        System.out.println("执行了test方法带char参数"+c);
      //    }
          public void test(String s){
              System.out.println("执行了test方法带String参数"+s);
          }
      
      	
          public static void main(String[] args){
              TestOverLoad to = new TestOverLoad();
              //不提供char类型的方法,也能执行成功,会执行int的方法
              //方法参数传递,类型之间的转化问题
              to.test('a');
          }
      }
      
      运行结果:执行了test方法带char参数a
      
      public class TestOverLoad {
          public void test(){
              System.out.println("执行了test方法没有携带参数");
          }
          public void test(boolean b){
              System.out.println("执行了test方法带boolean参数"+b);
          }
      //    public void test(int i){
      //        System.out.println("执行了test方法带int参数"+i);
      //    }
          public void test(char c){
              System.out.println("执行了test方法带char参数"+c);
          }
          public void test(String s){
              System.out.println("执行了test方法带String参数"+s);
          }
      
      
          public static void main(String[] args){
              TestOverLoad to = new TestOverLoad();
              to.test((char)99);
          }
      }
      
      运行结果:执行了test方法带char参数c
      
    4. jdk1.5版本之后,出现了一个新的写法

      int...x 动态参数列表,类型固定,个数可以动态 0-n 都可以

      x本质上就是一个数组,有length属性,有[index]

      动态参数列表的方法,不能与相同意义的数组类型的方法构成方法重载,本质是一样的

      动态参数列表的方法,可以不传参数,相当于0个

      数组的方法,必须传递参数

      动态参数列表在方法的参数中只能存在一份且必须放在方法参数的末尾

      public class TestOverLoad {
          public void test(int a,int...x){
          //public void test(int...x){ //int...x ->x是动态列表,本质是数组 int[] x = {1,2,3,4,5,6};
              System.out.println("执行了test方法携带动态列表");
              for(int i:x){
                  System.out.println(i);
              }
          }
      
          public void test(){
              System.out.println("执行了test方法没有携带参数");
          }
      //    public void test(boolean b){
      //        System.out.println("执行了test方法带boolean参数"+b);
      //    }
      ////    public void test(int i){
      ////        System.out.println("执行了test方法带int参数"+i);
      ////    }
      //    public void test(char c){
      //        System.out.println("执行了test方法带char参数"+c);
      //    }
      //    public void test(String s){
      //        System.out.println("执行了test方法带String参数"+s);
      //    }
      //
      //
          public static void main(String[] args){
              TestOverLoad to = new TestOverLoad();
              //to.test();
              to.test(1,2,3,4,5,6);
          }
      }
      
      to.test();运行结果:
      执行了test方法没有携带参数
      
      to.test(1,2,3,4,5,6);运行结果:
      执行了test方法携带动态列表
      2
      3
      4
      5
      6
      
    更多学习笔记移步 https://www.cnblogs.com/kknote
  • 相关阅读:
    SharePoint Error occurred in deployment step 'Recycle IIS Application Pool': 0x80070005:拒绝访问
    Getting SharePoint objects (spweb, splist, splistitem) from url string
    SharePoint 2010用“localhost”方式访问网站,File not found问题处理方式
    在 SharePoint 2010 打开网页出错时,显示实际的错误信息
    解决 SharePoint 2010 拒绝访问爬网内容源错误的小技巧(禁用环回请求的两种方式)
    SQL Server 删除数据库所有表和所有存储过程
    SQL Server 查询数据库表的列数
    SQL Server 自定义字符串分割函数
    sp_configure命令开启组件Agent XPs,数据库计划(Maintenance Plan)
    SQL Server 2008 收缩日志(log)文件
  • 原文地址:https://www.cnblogs.com/kknote/p/12933364.html
Copyright © 2011-2022 走看看