下列程序的输出结果为 class Box{ int length,width,height; public void setInfo(int l,int w,int h){ length = l; width = w; height = h; } public int volumn(){ return length*width*height; } public int area(){ return (length*width + length*height + width*height) * 2; } public String toString(){ return "Length:" + length + " width:" + width + " height:" + height + " volumn: " + volumn() + " area:" + area(); }} public class BoxTest { public static void main(String[] args) { Box b = new Box(); b.setInfo(5,2,4); System.out.println(b.toString()); }}
举一反三
- 【填空题】用拷贝构造函数进行对象构造。 #include<iostream> using namespace std; class Box { public: Box(int h,int w,int len); //构造函数声明 ( 1 ) //拷贝构造函数声明 int volume(); //求立方体体积函数 private: int height; int width; int length; }; Box::Box(int h,int w,int len) { height=h; width=w; length=len; } Box::Box(Box &a) { height=a.height+5; width=a.width+5; length=a.length+5; } int Box::volume() { return(height*width*length); } void main() { Box box1(5,5,5); cout<<"The volume of box
- 有以下定义: class Box{ int width,length,height; public: void set(int x=0,int y=0,int z=0) {width=x;length=y;height=z;} }; Box *box; 则以下哪种使用是正确的___
- 【多选题】已知代码如下 class Rectangle { private int width, height; public void setSize(int width, int height) { this.width = width; this.height = height; } } 下面哪些代码重载 setSize 方法 (10.0分) A. protected void setSize(int width, int height) { this(width, height) } B. public void setSize(int width, float height) { this.width = width; this.height = (int)height; } C. protected void setSize(int width) { this.width = width; } D. public void setSize(int height, int width) { this.width = width; this.height = height; }
- 类Rectangle中,width和height称为( )变量或( )变量。 public class Rectangle{ int width; int height; public void show(){ System.out.println("宽:"+width",高:"+height); } }
- Rectangle类中的成员变量有( )和( ) public class Rectangle{ int width; int height; public void setWidth(int x){ width = x; } public void setHeight(int y){ height = y; } public void show(){ System.out.println("宽:"+width",高:"+height); } }