java定义矩形类rect_Java定义矩形类矩形类package cn.p;写给女朋友的情书
public class Rectangle {
float width,height;
/**
* 计算⾯积
* @return 矩形的⾯积
*/
public float getArea(){
return width*height;
感恩陪伴
}
绝处逢生/**
* 计算周长全角数字
* @return 矩形的周长
*/
public float getCircumference(){
return 2*(width+height);
闭袢性肠梗阻
}
/**
* 改变矩形的⼤⼩
白沥岛
* @param w 宽度
* @param h ⾼度
*/
public void resize(float w,float h){
width = w;
height = h;
}
/**
* 输出矩形的宽和⾼
*/
public void printSize(){
蚊子咬了怎么办System.out.println("宽:"+width+" ⾼:"+height);
}
public static void main(String[] args) {
广州哪里比较好玩
Rectangle r1 = new Rectangle();
Rectangle r2 = new Rectangle();
r1.printSize();
r2.printSize();
System.out.println("矩形1的⾯积:"+r1.getArea());
System.out.println("矩形2的⾯积:"+r2.getArea());
System.out.println("矩形1的周长:"+r1.getCircumference()); System.out.println("矩形2的周长:"+r2.getCircumference()); }
}