新建Rectangle类;
代码如下:
package application;
class Rec{
double width;
double height;
public Rec(double w,double h){
width=w;
height=h;
}
public void computeCircum(){
double circum=2*(width+height);
System.out.println("该矩形的周长为"+circum);
}
public void computArea(){
double area=width*height;
System.out.println("该矩形的面积为"+area);
}
}
public class Rectangle{
public static void main(String[] args){
Rec output=new Rec(60,50);
output.computeCircum();
output.computArea();
}
}
本文介绍了如何在Java中创建Rectangle类,包括定义类的属性如宽度和高度,以及实现计算矩形周长和面积的方法。通过实例化Rectangle类并调用其方法,可以输出特定尺寸矩形的周长和面积。


被折叠的 条评论
为什么被折叠?



