1 //1)私有属性:上月电表读数、本月电表读数
2 //2)构造方法:无参、2个参数
3 //3)成员方法:getXXX()方法、setXXX()方法
4 //4)成员方法:显示上月、本月电表读数
5
6 public class spend {
7 private float lastm;
8 private float thism;
9
10 public spend(){
11 }//无参构造方法
12
13 public spend(int lastm,int thism){
14 this.lastm=lastm;
15 this.thism=thism;
16 }//含参构造
17
18 public void getl(){
19 System.out.println("上月电表读数为"+lastm) ;
20 System.out.println("上月电费为"+lastm*1.2) ;
21 }
22 public void gett(){
23 System.out.println("本月电表读数为"+thism) ;
24 System.out.println("上月电费为"+thism*1.2) ;
25 }//s1所用
26
27
28 public void setl(int lastm){
29 this.lastm=lastm;
30 System.out.println("上月电表读数为"+lastm) ;
31 System.out.println("上月电费为"+lastm*1.2) ;
32 }
33 public void sett(int thism){
34 this.thism=thism;
35 System.out.println("本月电表读数为"+thism) ;
36 System.out.println("上月电费为"+thism*1.2) ;
37 }
38
39 //创建对象一:上月电表读数为1000,本月电表读数为1200。
40 // 要求:调用无参构造方法创建对象;
41 // 调用setXXX()方法初始化对象;
42 // 假设每度电的价格为1.2元,计算并显示本月电费。
43 //创建对象二:上月电表读数1200,本月电表读数为1450。
44 // 要求:调用2个参数的构造方法创建并初始化对象;
45 // 调用setXXX()方法修改本月电表读数为1500(模拟读错了需修改);
46 // 假设每度电的价格为1.2元,计算并显示本月电费。
47
48
49 public static void main (String[] args){
50 spend s1 = new spend(1000,1200);
51 spend s2 = new spend();
52 s1.getl();
53 s1.gett();
54 s2.setl(1200);
55 s2.setl(1500);
56 }
57 }