JAVA 含义

来源:百度知道 编辑:UC知道 时间:2024/05/10 10:14:03
package zuoye;

public class Zuoye18 {

private float length;
private float width;
public Zuoye18(float length,float width){
this.length=length;
this.width=width;
}
public float acreage(){
return length*width;
}
public static void main(String args[]){
Zuoye18 i=new Zuoye18(3.4f,2.8f);
System.out.println(i.acreage());
}
}
麻烦给解释下每行代码的含义 为什么这么定义

public class Zuoye18 {

private float length;//定义私有属性float型的length
private float width;//定义私有属性float型的 width
public Zuoye18(float length,float width){//构造方法中有两个参数:float型的length和float型的width
this.length=length;//将传入的参数length赋值给类内部变量this.length(this表示该对象的内部)
this.width=width;//同上
}
public float acreage(){//定义float型返回值的方法acreage()
return length*width;//返回值为length*width
}
public static void main(String args[]){//main方法
Zuoye18 i=new Zuoye18(3.4f,2.8f);//调用类Zuoye18的构造方法构造一个名为i的对象,参数为3.4f和2.8f
System.out.println(i.acreage());//输出对象i的方法acreage()的返回值
}
}

//长方形(也包括正方形)类.
public class Zuoye18 {
private float length; // 长方形的宽
private float width; // 长方形的长
//长方形的构造方法,(也就是说输入长和宽就能形成一个实际的长方形)
public Zuoye18(float length,float width){
this.length=length;
this.width=width;
}
//这个方法是计算面积的...
public float acreage(){