java基础_重载

package java_test;

class  Student{
    public void max(int a,int b){
        System.out.println(a>b?a:b);//a>b的话返回a,否则返回b
    }
    
    public void max(int a,int b,int c){
        double max =(a>b?a:b);//a>b的话返回a,否则返回b
        System.out.println(max>c?max:c);
    }
}

public class Test111 {
    public static void main(String[] args){
        Student one = new Student();
        one.max(3, 4, 1);
    }
}

一个类中的方法名相同,但是参数不同,没毛病。调方法的时候,看参数是什么,根据参数区分调用的是哪个方法。

原文地址:https://www.cnblogs.com/youning/p/6698587.html