• 2022-05-31
    下列程序的输出结果为 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()); }}
  • Length:5 width:2 height:4 volumn: 40 area:76

    举一反三

    内容

    • 0

      阅读下列程序,写出程序运行的结果:class Cube{int width;int height;int depth;Cube(int x,int y,int z){this.width=x;this.height=y;this.depth=z;}public int vol(){return width*height*depth;}}public class UseCube {public static void main(String[] args) {Cube a=new Cube(3,4,5);System.out.println("长度="+a. width);System.out.println("体积="+a.vol());}}

    • 1

      中国大学MOOC: 已知:class Rectangle { private int width, height; public void setSize(int width, int height) { this.width = width; this.height = height; }}下面哪些代码重载 setSize 方法

    • 2

      【单选题】定义描述矩形的类 Rectangle ,描述长方体高的类 High ,其数据成员为长方体高度 H 。再由矩形类与高类多重派生出长方体类 Cuboid 。主函数中定义长方体对象并显示数据。 #include class Rectangle { protected: float Length,Width; // 数据成员为长与宽,类外不可访问 public: float Area() // 计算矩形面积的函数 { return Length*Width; } Rectangle(float L,float W ) { Length=L; Width=W; } Rectangle() { Length=0; Width=0; } }; class High{ private: float Height; // 数据成员为高度, public: High(float x=0) // 构造函数 { Height =x; } float GetH() { return Height; } }; class C A. Volume=Area()*GetH() ; B. Volume= Length*Width*Height ; C. Volume= Length*Width *GetH() ; D. Vol() ;

    • 3

      有以下定义:classBox{intwidth,length,height;public:voidset(intx=0,inty=0,intz=0){width=x;length=y;height=z;}};Box*box;则以下哪种使用是正确的___ A: box->width=3; B: cout<height; C: box->set(1,2); D: p.set(1,2);

    • 4

      下列哪些是正确的声明变量的方式? A: int length; int width; B: int length, width; C: int length; width; D: int length, int width;