标签:ring inter main class ice return imp 使用 ace
贷款购买房屋时,支付首付款是一种功能操作,定义在Payment接口中。住宅产权房屋类和商业产权房屋类均具有支付首付款的功能。依据以上内容,创建一个接口和两个类:
接口Payment
package com;
public interface Payment {
double downPay(double price,double area);
}
Payment接口的非抽象使用类——Residence类(住宅产权房屋类)
package com;
public class Residence {
double downPay(double price,double area){
return price*area*0.2;
}
}
Payment接口的非抽象使用类——Commercial类(商业产权房屋类)
package com;
public class Commercial {
double downPay(double price,double area){
return price*area*0.45;
}
}
在主类Test中,创建住宅产权房屋对象,完成住宅产权房屋首付款的计算。再将定义商业产权房屋对象传递给Payment接口对象,之后利用接口回调完成商业产权房屋首付款的计算。输入测试数据,完成结果的输出。
package com;
import java.util.Scanner;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
Residence residence=new Residence();
Commercial commercial =new Commercial();
Scanner reader =new Scanner(System.in);
System.out.println("输入房屋每平米价格");
double price =reader.nextDouble();
System.out.println("输入房屋面积");
double area =reader.nextDouble();
System.out.println("住宅产权房屋的首付:"+residence.downPay(price, area));
System.out.println("商业产权房屋的首付:"+commercial.downPay(price, area));
}
}
标签:ring inter main class ice return imp 使用 ace
原文地址:https://www.cnblogs.com/hzcxwz/p/11809016.html