接口和抽象类练习题----计算电费时区分冬季和夏季用电情况

2019-04-13 14:10发布

/* 电在传输上都会有损耗,假如计算电费时区分冬季和夏季用电情况,冬季在用电原有读数上加上20千瓦小时作为用电损耗,而夏季没有此损耗问题。电费是每千瓦小时电0.49元,假定用电度数为125千瓦小时,创建一个抽象类Season ,有抽象方法calcFee(int  i),分别计算出冬季Winter和夏季Summer相应的电费,最后创建一个工具类FeeUtil,有方法fee(Season s,int count)计算并打印电费。*///创建一个抽象类Seasonpublic abstract class Season { public abstract double calcFee(int i); }
//创建一个冬季Winter类public class Winter extends Season { @Override public double calcFee(int i) { // TODO Auto-generated method stub return (i +20)*0.49; } }
//创建一个夏季Summer类
public class Summer extends Season { @Override public double calcFee(int i) { // TODO Auto-generated method stub return i*0.49; } }
//最后创建一个工具类FeeUtil,有方法fee(Season s,int count)计算并打印电费。public class FeeUtil{ public void fee(Season s,int count){ System.out.println("电费是:" + s.calcFee(count)); } }
// 测试类:public class Test03 { public static void main(String[] args) { // TODO Auto-generated method stub Winter w = new Winter(); System.out.println("冬季电费:" + w.calcFee(20)); Summer s = new Summer(); System.out.println("夏季电费:" + s.calcFee(20)); FeeUtil f = new FeeUtil(); System.out.print("冬季---"); f.fee(new Winter(), 5); System.out.print("夏季---"); f.fee(new Summer(), 5); } }