zoukankan      html  css  js  c++  java
  • 方法重载(一)

    • 方法重载:指在同一个类中,允许存在一个以上的同名方法,只要它们的参数列表不同即可,与修饰符和返回值类型无关。

    • 参数列表:数据类型个数不同,数据类型不同,数据类型顺序不同。

    • 重载方法调用:JVM通过方法的参数列表,调用不同的方法。

      #### 方法重载练习一:比较两个数据是否相等
      
      比较两个数据是否相等。参数类型分别为两个`byte`类型,两个`short`类型,两个`int`类型,两个`long`类型,并在`main`方法中进行测试。
      public class Method_Demo6 {
          public static void main(String[] args) {
              //创建
              Count c = new Count();
              
              //定义不同数据类型的变量
              byte a = 10;
              byte b = 20;
              short c = 10;
              short d = 20;
              int e = 10;
              int f = 10;
              long g = 10;
              long h = 20;
              // 调用
              System.out.println(c.compare(a, b));
              System.out.println(c.compare(c, d));
              System.out.println(c.compare(e, f));
              System.out.println(c.compare(g, h));
          }
      }
      
      class Count {
              // 两个byte类型的
          public boolean compare(byte a, byte b) {
              System.out.println("byte");
              return a == b;
          }
      
          // 两个short类型的
          public boolean compare(short a, short b) {
              System.out.println("short");
              return a == b;
          }
      
          // 两个int类型的
          public boolean compare(int a, int b) {
              System.out.println("int");
              return a == b;
          }
      
          // 两个long类型的
          public boolean compare(long a, long b) {
              System.out.println("long");
              return a == b;
          }
      }
  • 相关阅读:
    C# IEnumerable 和 IEnumerator接口浅析
    SQLite笔记
    命令行工具osql.exe使用
    2016年年终工作总结
    c# Json 自定义类作为字典键时,序列化和反序列化的处理方法
    多线程随笔
    常见异步机制分析
    SQL 通过syscolumns.xtype动态查找指定数据类型字段所包含的数据
    SQL 删除索引错误
    SQL 实用函数
  • 原文地址:https://www.cnblogs.com/panyizuoshan/p/11398308.html
Copyright © 2011-2022 走看看