1个回答
2016-07-01
展开全部
接口:FactorialImp抽象类:FactorialAbs实现计算阶乘n!的类:Fatorial代码:/** * * 阶乘计算器 * * 接口 */public interface FactorialImp {// 计算阶乘n!的值long factorial(int n);}/** * * 阶乘计算器 * * 抽象类 继承 FactorialImp */public abstract class FactorialAbs implements FactorialImp {/** * 实现计算阶乘n!的值的方法 */public long factorial(int n) {return multiplicationCount(n, n - 1);}/** * 增加抽象方法--计算两数相乘 * * @param param1Int * @param param2Int * @return 两数相乘的积 */abstract long multiplicationCount(long param1Int, long param2Int);}/** * 实现阶乘计算器类 * */public class Fatorial extends FactorialAbs {@Overridelong multiplicationCount(long param1Int, long param2Int) {if (param2Int == 1) {return param1Int;} else {return multiplicationCount(param1Int * param2Int, param2Int - 1);}}}/** * 测试类 * */public class Test {public static void main(String[] args) {Fatorial localFatorial = new Fatorial();System.out.println("100! = " + localFatorial.factorial(10));}}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询