JAVA复习 类与对象

总体框架

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关键字修饰,则调用方法为
		//类名.方法名
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值