方法的重载讲解
定義:同一個(gè)類,方法名稱相同,形參不同
規(guī)則:
方法名稱必須相同;
參數(shù)列表必須不同(個(gè)數(shù)不同或類型不同或參數(shù)排列順序不同);
與返回值類型無關(guān)
package com.wuming.struct;
public class Demo03 {
? ? public static void main(String[] args) {
? ? ? ? double max=max(10,20);
? ? ? ? System.out.println(max);
? ? }
? ? //比大小0
? ? public static int max(int num1,int num2){
? ? ? ? int result=0;
? ? ? ? if(num1==num2){
? ? ? ? ? ? System.out.println("num1==num2");
? ? ? ? ? ? return 0;//終止方法
? ? ? ? }
? ? ? ? if (num1>num2){
? ? ? ? ? ? result=num1;
? ? ? ? }else{
? ? ? ? ? ? result=num2;
? ? ? ? }
? ? ? ? return result;
? ? }
? ? //比大小0
? ? public static double max(double num1,double num2){
? ? ? ? double result=0.0;
? ? ? ? if(num1==num2){
? ? ? ? ? ? System.out.println("num1==num2");
? ? ? ? ? ? return 0;//終止方法
? ? ? ? }
? ? ? ? if (num1>num2){
? ? ? ? ? ? result=num1;
? ? ? ? }else{
? ? ? ? ? ? result=num2;
? ? ? ? }
? ? ? ? return result;
? ? }
}
20.0
總結(jié)
- 上一篇: Python input 函数 - Py
- 下一篇: React之事件绑定