1 package com.lo; 2 3 public class juxing { 4 5 private double chang; 6 7 private double kuan; 8 9 public double getChang() 10 { 11 return chang; 12 } 13 public double getkuan() 14 { 15 return kuan; 16 } 17 public void setChang(double chang) 18 { 19 if (chang >0) 20 { 21 this.chang = chang; 22 } 23 else 24 { 25 System.out.println("长度值 " + chang + "无效"); 26 } 27 } 28 public void setKuan(double kuan) 29 { 30 if(kuan<=0) 31 { 32 System.out.println("宽度值 "+ kuan + "无效"); 33 } 34 else 35 { 36 this.kuan = kuan; 37 } 38 } 39 public juxing() 40 { 41 42 } 43 public juxing(double chang,double kuan) 44 { 45 setChang(chang); 46 setKuan(kuan); 47 } 48 public double getMianji() 49 { 50 return this.chang * this.kuan; 51 } 52 public static void main(String[]args) 53 { 54 juxing myJX = new juxing(2,7); 55 56 myJX.chang = 12; 57 58 System.out.println("矩形的面积 = "+myJX.getMianji()); 59 } 60 }
package com.lo;
public class text1 {
public static void main(String[] args) {
juxing myJX1 = new juxing(20,6);
System.out.println("长 = "+myJX1.getChang());
System.out.println("宽 = "+myJX1.getkuan());
System.out.println("矩形面积 ="+myJX1.getMianji());
myJX1.setChang(30);
myJX1.setKuan(15);
System.out.println("矩形面积 ="+myJX1.getMianji());
}
}