要求:调用无参构造方法创建对象;
调用setXXX()方法初始化对象;
假设每度电的价格为1.2元,计算并显示本月电费。
要求:调用2个参数的构造方法创建并初始化对象;
调用setXXX()方法修改本月电表读数为1500(模拟读错了需修改);
假设每度电的价格为1.2元,计算并显示本月电费
class Cost{ private double lastCost; private double thisCost; Cost() { } Elc(double lastCost,double thisCost) { this.lastCost=lastCost; this.thisCost=thisCost; } double getlastCost() { return this.lastCost; } double getthisCost() { return this.thisCost; } void setlastCost(double lastCost) { this.lastCost=lastCost; } void setthisCost(double thismElc) { this.thisCost=thisCost; } void showCost() { System.out.println("上月电表读数为:"+this.lastCost); System.out.println("本月电表读数为:"+this.thisCost); } } public class CostTest { public static void main(String[] args) { Cost a=new Cost(); a.setlastCost(1000); a.setthisCost(1200); a.showCost(); System.out.println("本月电费为:"+(a.getthisCost()-a.getlastCost())*1.2); Cost b=new Cost(1200,1450); b.setthisCost(1500); b.showCost(); System.out.println("本月电费为:"+(b.getthisCost()-b.getlastCost())*1.2); } }
原文:https://www.cnblogs.com/ShijouTakane/p/11137006.html