java方法重载

 方法的重载的条件  1. 需要在同一个类里面  2. 方法名称要一致  3.参数列表不同(参数类型 ,个数,顺序)



public class Vartext22 {

  public static void main (String[] args) {

  System.out.println(student(0));

  System.out.println(student(10000 , 20000));

  System.out.println(student(1.2f , 1.6f));

  System.out.println(student(10.22 , 19.2));

  }





  public static  boolean student (int i) {

if (i == 0) {

  return true;

} else {

return false;

}

  }


  public static long student (long f , long b) {

return f + b;

  }


  public static float student (float a, float b) {


return  a + b;

  }


  public static double student (double b , double c) {

return  b + c;

  }

}

// 参数个数不同 m1方法构成重载

public static void m1 () {}

public static void m1 (int  i) {}




// 参数类型不同  m2方法构成重载

public static void m2 (int i) {}

public static void m2 (long l) {}

// 参数顺序不同  m3方法构成重载  但是开发当中 不这样使用

public static void m3 (int i ,long l) {}

public static void m3 (long l , int i) {}

// 错误写法  方法重名

public static void m4 () {}

public static void m4 () {}

// (注意 返回值类型跟方法重载没有关系  不构成重载 编译报错)

public static void m5 () {}

public static int m5 () {

return 1;

}

// 重载跟修饰符列表的不同没有关系 编译报错

public static void m6 () {}

public void m6 () {}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容