constructor_overloading.java
来自「JAVA程序设计课程中各章节的程序实例。」· Java 代码 · 共 52 行
JAVA
52 行
//Overloading constructor
class Box
{
double width;
double height;
double depth;
Box(double w, double h, double d)
{
width = w;
height = h;
depth = d;
}
Box()
{
width = -1;
height = -1;
depth = -1;
}
Box(double len)
{
width = height = depth = len;
}
double volume()
{
return width*height*depth;
}
}
class Constructor_overloading
{
public static void main(String args[])
{
Box mybox1 = new Box(10,20,25);
Box mybox2 = new Box();
Box mycube = new Box(7);
double vol;
vol = mybox1.volume();
System.out.println(vol);
vol = mybox2.volume();
System.out.println(vol);
vol = mycube.volume();
System.out.println(vol);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?