总体框架
public class Rectangle()
{
1\private double width;
2\private double length;
3\public Rectangle(double width,double length)
4\public Rectangle(){}//不带参数的构造方法
5\public void output(){输出}
6\public double perimeter(){}
7\public double area(){}
8\public void staic void main(){函数的调用}
}
package ch01;
//JAVA编程
import java.util.Scanner;//Scanner大写
public class Rectangle {
//属性成员
private double width;
private double length;
//利用width和length创建构造方法
public Rectangle(double width,double length)
{
this.length=length;
this.width=width;
}
//不带参数的构造方法
public Rectangle() {
//省略get和set方法
}
//输出长方形的长宽信息
public void output()
{
System.out.println("长方形的长为:"+length);
System.out.println("长方形的宽为"+width);
System.out.println("长方形的面积为"+area());
System.out.println("长方形的周长为"+perimeter());
}
//计算长方形的周长
public double perimeter()
{
return 2*width*length;
}
public double area()
{
return 2*(length+width);
}
public static void main(String[] args)
{
Scanner scanner=new Scanner(System.in);
System.out.println("please input Rectangle's length:");
double length=scanner.nextDouble();
System.out.println("please input Rectangle's width:");
double width=scanner.nextDouble();
//利用构造方法创建一个Rectangle类型的对象
Rectangle rectangle=new Rectangle(width,length);
rectangle.output();//方法的调用形式为对象.方法名;如果该方法是有static关键字修饰,则调用方法为
//类名.方法名
}
}