定义一个接口,再定义类实现该接口,编写应用程序,调用接口中的 3 个方法,并将调用方法所得的结果输出。

来源:互联网 发布:php 对象类型 编辑:程序博客网 时间:2024/04/26 06:22
/**
 * 定义一个接口,接口中有 3 个抽象方法如下。
(1)“long fact(int m);”方法的功能为求参数的阶乘。
(2)“long intPower(int m,int n);”方法的功能为求参数 m 的 n 次方。
(3)“boolean findFactor(int m,int n);”方法的功能为判断参数 m 加上参数 n 的和是否大
于100。
定义类实现该接口,编写应用程序,调用接口中的 3 个方法,并将调用方法所得的结果输出。
 * @author Chenkunqing
 *time:2017/7/26/21:00
 */
interface MathInterface{
long fact(int m);
long intPower(int m,int n);
boolean findFactor(int m,int n);
}


class MathInter implements MathInterface {
/**
* 方法的功能为求参数的阶乘。
* @author Chenkunqing
*
*/
public long fact(int m){
int sum =1;
for(int i=1 ;i<=m;i++){
sum *=i;
}
return sum;
//定一个数用于存储结果。

};
/**
* 方法的功能为求参数 m 的 n 次方。
* @author Chenkunqing
*
*/
public long intPower(int m,int n){
int cj=1;
for(int i=0;i<n;i++){
cj=(int) Math.pow(m,n); 
//求一个数,如m的n次方
}
return cj;

};
/**
* 方法的功能为判断参数 m 加上参数 n 的和是否大于100。
* @author Chenkunqing
*
*/
public boolean findFactor(int m,int n){
if((m+n)>100){
return true;
}else{
return false;
}
//如果是真的大于就返回true,反之,不是返回false。
};


}
public class TestInterface {


public static void main(String[] args) {
// TODO Auto-generated method stub
MathInter mi=new MathInter();
mi.fact(10);
mi.findFactor(30, 80);
mi.intPower(2, 3);
System.out.println(mi.fact(10));
System.out.println(mi.findFactor(30, 80));
System.out.println(mi.intPower(5, 3));
}


}
阅读全文
0 0