关于构造函数的方法 Cube

@TOC关于构造函数的方法 Cube
这个方法将main方法运用到了另一个java文件中。
package util;

public class Cube {
private int length;
private int width;
private int depth;

public int calVolume() {
	int vol=length*width*depth;
	return vol;
}
public int calArea() {
	int area=(length*width+width*depth+length*depth)*2;
	return area;
}
public int getLength() {
	return length;
}
public int getWidth() {
	return width;
}
public int getDepth() {
	return depth;
}
public void setLength(int length) {
	this.length = length;
}
public void setWidth(int width) {
	this.width =width;
}
public void setDepth(int depth) {
	this.depth =depth;
}
public void inlarge(int scale) {
	length*=scale;
	width*=scale;
	depth*=scale;
}

}
Test cube:
public class Test {
public static void main(String ar[]) {
Cube c=new Cube();
c.setLength(1); //引用“立方体长”的方法,给长赋值为1
c.setWidth(2);
c.setDepth(3);
System.out.println(“长=”+c.getLength()+",宽="+c.getWidth()+",高="+c.getDepth()); //输出长,宽,高的值
System.out.println(“表面积为”+c.calArea()); //输出面积的值
System.out.println(“体积为”+c.calVolume()); //输出体积的值
c.inlarge(3); //引用cube中的inlarge方法
System.out.println(“长=”+c.getLength()+",宽="+c.getWidth()+",高="+c.getDepth()); //同上
System.out.println(“表面积为”+c.calArea());
System.out.println(“体积为”+c.calVolume());
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值