Java编写一个Rect类
该类包含(1)2个私有属性,长length和宽width.(2)一个构造器方法,带两个参数,用于对length和width属性进行初始化.(3)length和width属...
该类包含(1)2个私有属性,长length和宽width.(2)一个构造器方法,带两个参数,用于对length和width属性进行初始化.(3)length和width属性的set和get方法.(4)2个公有成员方法.分别用于计算并返回矩形的面积和周长.
展开
1个回答
展开全部
public class Rect{
private int length;
private int width;
public Rect(int length, int width) {
this.length = length;
this.width = width;
}
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public void countArea(){
int area = this.length * this.width;
System.out.println("矩形的面积为 : "+area);
}
public void countGirth(){
int girth = (this.length + this.width) * 2;
System.out.println("矩形的周长为 : "+girth);
}
public static void main(String[] args) {
Rect rect = new Rect(20, 30);
rect.countArea();
rect.countGirth();
}
}
private int length;
private int width;
public Rect(int length, int width) {
this.length = length;
this.width = width;
}
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public void countArea(){
int area = this.length * this.width;
System.out.println("矩形的面积为 : "+area);
}
public void countGirth(){
int girth = (this.length + this.width) * 2;
System.out.println("矩形的周长为 : "+girth);
}
public static void main(String[] args) {
Rect rect = new Rect(20, 30);
rect.countArea();
rect.countGirth();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询