refdemo.java

来自「JAVA程序设计课程中各章节的程序实例。」· Java 代码 · 共 67 行

JAVA
67
字号
//Sample2
class Box
{
	double width;
	double height;
	double depth;

	Box()
	{
		width = height = depth = -1;
	}

	Box(double len)
	{
		width = height = depth = len;
	}

	Box(double w, double h, double d)
	{
		width = w;
		height = h;
		depth = d;
	}

	double volume()
	{
		return width*height*depth;
	}	
}

class boxWeight extends Box
{
	double weight;

	boxWeight(double w, double h, double d, double wh)
	{
		width = w;
		height = h;
		depth = d;
		weight = wh;
	}

	double volume()
	{
		return width*height*depth*weight;
	}
}

class refDemo
{
	public static void main(String args[])
	{
		boxWeight weightbox = new boxWeight(1,2,3,4);
		Box plainbox = new Box();
		double vol;

		vol = weightbox.volume();
		System.out.println(vol);
		System.out.println("Weight:" + weightbox.weight);
//		weightbox = plainbox;2004.10.22
//		vol = weightbox.volume();这样是不行的!!!
		plainbox = weightbox;
		vol = plainbox.volume();
		System.out.println(vol);
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?