君子攸宁

导航

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);
    }
}

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

posted on 2017-04-12 12:21  君子攸宁  阅读(97)  评论(0编辑  收藏  举报