java方法使用

1 在方法中可以调用同一个类中的方法和属性,但是不能定义方法。system.out语句只能在方法中

2 方法重载:(1)同一个类中 (2)方法名相同 (3)参数列表的类型不同或者个数不同 

代码:
public class Method1 {
//方法重载求2个数的最大值
    public static void main(String[] args) {
        Method1 m = new Method1();
        System.out.println(m.max(34, 89));
        System.out.println(m.max(32.4, 32.1));
        System.out.println(m.max(546, 345, 554));
       

    }
    public int max(int a,int b){
        return (a>b)? a : b;
    }
    public double max(double a,double b){
        return (a>b)? a : b;
    }
    public double max(double a,double b,double c){
        return (max(a,b)>c)? max(a,b) : c;
    }
}

原文地址:https://www.cnblogs.com/yjtm53/p/4125362.html