9.22 周五

// MethodOverload.java
// Using overloaded methods

public class MethodOverload {

public static void main(String[] args) {
System.out.println("The square of integer 7 is " + square(7));
System.out.println("\nThe square of double 7.5 is " + square(7.5));
}

public static int square(int x) {
return x * x;
}

public static double square(double y) {
return y * y;
}
}

运用了方法重载,,满足了,方法名相同,参数类型不同,参数个数不同,参数类型的顺序不同

返回值类型不同不可以作为方法重载的条件。

import java.math.BigInteger;
import java.util.Scanner;


public class CalculateN {

/**
* @param args
*/
public static void main(String[] args) {
System.out.print("请输入N:");
Scanner scanner=new Scanner(System.in);
int number=scanner.nextInt();
System.out.println(number+"!="+calculateN2(number));

}

public static long calculateN(int n) {
if(n==1 || n==0){
return 1;
}

return n*calculateN(n-1);
}

public static BigInteger calculateN2(int n) {
if(n==1 || n==0){
return BigInteger.valueOf(1);
}
return BigInteger.valueOf(n).multiply(calculateN2((n-1)));
}
}

posted @   liuxuechao  阅读(9)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示