package study;//创建类
/*
* 定义一个矩形类Rectangle:(知识点:对象的创建和使用)
*1 定义三个方法:getArea()求面积、getPer()求周长,showAll()分别在控制台输出长、宽、面积、周长。
*2 有2个属性:长length、宽width
*3 创建一个Rectangle对象,并输出相关信息
*/
public class Rectangle {
double length;
double width;
public void getArea(){
System.out.println("面积是:"+length*width);
}
public void getPer(){
System.out.println("周长是:"+(length+width)*2);
}
public void showAll(){
System.out.println("长是:"+length);
System.out.println("宽是:"+width);
getArea();
getPer();
}
}
package study;//创建类
/*
* 定义一个矩形类Rectangle:(知识点:对象的创建和使用)
*1 定义三个方法:getArea()求面积、getPer()求周长,showAll()分别在控制台输出长、宽、面积、周长。
*2 有2个属性:长length、宽width
*3 创建一个Rectangle对象,并输出相关信息
*/
public class Rectangle {
double length;
double width;
public void getArea(){
System.out.println("面积是:"+length*width);
}
public void getPer(){
System.out.println("周长是:"+(length+width)*2);
}
public void showAll(){
System.out.println("长是:"+length);
System.out.println("宽是:"+width);
System.out.println("面积是:"+length*width);
System.out.println("周长是:"+(length+width)*2);
}
}
package study;//创建对象
public class homework {
public static void main(String[] args) {
Rectangle Rectangle=new Rectangle();
Rectangle.length=3;
Rectangle.width=4;
Rectangle.showAll();
}
}
//运行结果
![]()