定义⼀个矩形类Rectangle:(知识点:对象的创建和使⽤)/**
孟晓驷1:定义⼀个矩形类Rectangle:(知识点:对象的创建和使⽤)
定义三个⽅法:getArea()求⾯积、getPer()求周长,showAll()分别在控制台输出长、宽、⾯积、周长。
有2个属性:长length、宽width
通过构造⽅法Rectangle(int width, int length),分别给两个属性赋值
创建⼀个Rectangle对象,并输出相关信息
*/
class Rectangle{
private int length;
private int width;
//构造⽅法
vsync
Rectangle(int length,int width){
this.length=length;
this.width=width;
}
//控制台显⽰长,宽,⾯积,周长
public void showAll(){
System.out.println("矩形长为:"+length+"\t"+"矩形的宽为:"+width+"\t"+"矩形的⾯积为:"+getArea()+"\t"+"矩形的周长为:"+getPer());
}牛津
//求周长jealousy
stunningpublic int getPer(){
return 2*(length+width);
}yege
//求⾯积
public int getArea(){
return length*width;mtm
agreementidesign}
//
}
class RectangleDemo{
public static void main(String[] args){
port什么意思Rectangle r=new Rectangle(2,3);
r.showAll();
}
}