编写一个矩形类,将长与宽作为矩形类的属性,在构造方法中将长、宽初始化,定义一个成员方法求此矩形面积
package lianxi; public class lei { double Chang; double Kuan; void M(double x1,double x2) { Chang = x1; Kuan=x2; double MianJi=Chang*Kuan; //double s=Chang*Kuan; System.out.println("矩形的面积="+MianJi); } lei(){ } lei(double y1,double y2){ Chang=y1; Kuan=y2; double mianji=Chang*Kuan; } double L(double t1,double t2){ return(t1*t2); } void LL(){ double mianji=Chang*Kuan; System.out.println("长方形的面积是"+mianji); } boolean lm(){ boolean n=true; if (Chang==0||Kuan==0){ n=false; System.out.println("这不是四边形"); }else{ System.out.println("四边形的面积是"+Chang*Kuan); }return n; } }
package lianxi; public class shiyonglei { public static void main(String[] args) { //无返回有参数 lei S=new lei(); S.M(23,23); System.out.println("长方形的面积是"+S.Chang*S.Kuan); //构造方法 lei Z=new lei(2,4); System.out.println("矩形的面积是"+Z.Chang*Z.Kuan); //有返回有参数 lei mm=new lei(); System.out.println("长方形的面积是"+mm.L(5, 8)); //无返回无参数 lei ll=new lei(); ll.Chang=23; ll.Kuan=2; ll.LL(); //有返回无参数 lei LM=new lei(); LM.Chang=3; LM.Kuan=4; System.out.println(LM.lm()); } }