实验三一、实验内容:三重继承:利用继承关系,定义点类、矩形类、长方体类,并创建测试这3个类的对象的主类。二、算法设计:①Point类的设计成员变量:intx,y;成员方法:setx,getx,sety,gety,toString,Point(),Point(intxValue,intyValue)②Rectangle的设计从Point类派生成员变量:intwidth,length成员方法:setWidth,getWidth,getLength,setLength,getPerimeter,getArea,toString,Rectangle(),Rectangle(intxValue,intyValue,intwidth,inlength)③Cube类的设计从Rectangle类派生成员变量:inthight成员方法:setHight,getHight,getArea,getVolume,toString,Cube(intxValue,intyValue,intwidth,intlength,inthight)三、运行结果:四、程序代码://Point.javapublicclassPoint{protectedintx;protectedinty;publicPoint(){}publicPoint(intxValue,intyValue){x=xValue;y=yValue;}publicvoidsetx(intxValue){x=xValue;}publicintgetx(){returnx;}publicvoidsety(intyValue){y=yValue;}publicintgety(){returny;}publicStringtoString(){return(+x+,+y+);}}//Rectangle.javapublicclassRectangleextendsPoint{protectedintwidth;protectedintlength;publicRectangle(){}publicRectangle(intxValue,intyValue,intwidth,intlength){x=xValue;y=yValue;setWidth(width);setLength(length);}publicvoidsetWidth(intwidth){this.width=width;}publicvoidsetLength(intlength){this.length=length;}publicintgetWidth(){returnwidth;}publicintgetLength(){returnlength;}publicintgetPerimeter(){return2*(width+length);}publicintgetArea(){returnwidth*length;}publicStringtoString(){return中心=(+x+,+y+);长=+length+;宽=+width;}}//Cube.javapublicclassCubeextendsRectangle{privateinthight;publicCube(intxValue,intyValue,intwidth,intlength,inthight){super(xValue,yValue,width,length);setHight(hight);}publicvoidsetHight(inthight){this.hight=hight;}publicintgetHight(){returnhight;}publicintgetArea(){return2*(hight*width+width*length+length*hight);}publicintgetVolume(){returnhight*width*length;}publicStringtoString(){returnsuper.toString()+;高=+getHight();}}//Shape.javaimportjavax.swing.JOptionPane;publicclassShape{publicShape(){}publicstaticvoidmain(String[]args){Cubeshape1=newCube(2,3,4,5,7);Stringoutput=X坐标:+shape1.getx()+\nY坐标:+shape1.gety()+\n长:+shape1.getLength()+\n宽:+shape1.getWidth()+\n高:+shape1.getHight();shape1.setx(4);shape1.sety(9);shape1.setLength(12);shape1.setWidth(7);shape1.setHight(10);output+=\n\n长方体新位置、长、宽、高依次是:\n+shape1.toString();output+=\n长方形周长:+shape1.getPerimeter();output+=\n长方体的表面积是:+shape1.getArea();output+=\n长方体的体积是:+shape1.getVolume();JOptionPane.showMessageDialog(null,output);}}五、心得体会:通过这次实验,我对Java的超类和子类有了新的理解和认识。通过多重继承,可以简化编程,继承除了保留父类的功能,我们还能在子类中拓展其新的功能。这次实验,虽然很简单,但是通过此次实验,我对类的知识重温了一下,加深了自己对类的印象。