class FunctionDemo2 { public static void main(String[] args){ juxing(5,4); } public static int getSun(int x,int y){ return (x+y); } public static boolean compare(int x,int y){ // if(x==y){ // return true; // } return x==y; } public static int getMax(int x,int y){ // if(x>=y){ // return x; // } // else return y; return (x>y)?x:y; } public static void juxing(int a,int b){ for(int x=0;x<a;x++){ for(int y=0;y<b;y++){ System.out.print("* "); } System.out.println(); } return ; } }
什么时候用重载?
当定义的功能相同,但参与运算的未知内容不同。
那么,这时就定义一个函数名称以表示其功能,方便阅读,而通过参数列表的不同来区分多个同名函数。
public static int add(int x,int y){ return x+y; } public static int add(int x,int y,int z){ return add(x,y)+z; }