一、题目 :贷款购买房屋时,支付首付款是一种功能操作,定义在Payment接口中。住宅产权房屋类和商业产权房屋类均具有支付首付款的功能。依据以上内容,创建一个接口和两个类:
二、源代码
Payment.java
/*接口Payment;*一个计算房屋首付款的方法downPay;*参数price代表房屋每平方米的价格;*area代表房屋的总平方数,即面积;*/package com; public interface Payment { public double downPay(double price,double area); }
Residence.java
/*重写计算房屋首付款的方法,并将结果返回*/package com; public class Residence implements Payment { public double downPay(double price, double area) { return price*area*0.2; } }
Commercial.java
/*重写计算房屋首付款的方法,并将结果返回*/package com; public class Commercial implements Payment{ @Override public double downPay(double price, double area) { return price*area*0.5; } }
Text.java
/*创建住宅产权房屋对象,完成住宅产权房屋首付款的计算
*定义商业产权房屋对象;
*接口回调完成商业产权房屋首付款的计算*/
package com;
import java.util.Scanner; public class Text { public static void main(String[] args) { Scanner reader=new Scanner(System.in); Residence a=new Residence(); Payment b=new Commercial(); System.out.println("房屋每平方米的价格"); double price=reader.nextDouble(); System.out.println("房屋的总面积"); double area=reader.nextDouble(); System.out.println("住宅产权房屋的首付为:"+a.downPay(price, area)); System.out.println("商业产权房屋的首付为:"+b.downPay(price, area)); } }