java题目,定义一个类,分别求长,宽分别是20,10 6,3的长方形的面积 在机房 老师布置作业 在线等

[复制链接]
查看11 | 回复1 | 2011-4-12 12:52:16 | 显示全部楼层 |阅读模式
成员变量
回复

使用道具 举报

千问 | 2011-4-12 12:52:16 | 显示全部楼层
<pre id=\"best-answer-content\" class=\"reply-text mb10\">public class Test {

public static void main(String[] args) {
Rectangle rectangle = new Rectangle(20, 10);
double area = rectangle.area();
System.out.println(\"Area for rectangle (20, 10) is: \" area);

Rectangle rectangle2 = new Rectangle(6, 3);
double area2 = rectangle2.area();
System.out.println(\"Area for rectangle (6, 3) is: \" area2);

}
}


class Rectangle{
private double width;
private double height;

public Rectangle(){
this(0, 0);
}

public Rectangle(double width, double height){
this.width = width;
this.height = height;
}

public double area(){
return width * height;
}
}

-------------
Area for rectangle (20, 10) is: 200.0
Area for rectangle (6, 3) is: 18.0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行