欢迎来到天天文库
浏览记录
ID:46689855
大小:112.00 KB
页数:9页
时间:2019-11-26
《实验5继承、基础类与包》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、实验5继承、基础类与包一、实验目的1、理解类的继承,终结类、终结方法、抽象类与抽象方法的概念。2、熟练掌握终结类、终结方法、抽象类与抽象方法的定义及实现方法。3、熟练掌握本书捉到的Java基础类库中的一些常见类,学会查阅JavaDoco4、了解Java包的概念及编译运行方法,养成良好的命名习惯。实验五第一题内容:1.题目:/*编写一抽彖类(Shape2D),*Circle、TritanglesRectangle均为莫子类。*其中父类中有计算其周长、面积的方法。然后编写一测试类,*分別建立若干个了对象,并分别将这些对象的面积与周长输出
2、。*/2•实验主要流程、基本操作或核心代码、算法片段(该部分如不够填写,请另加附页)publicabstractclassShape2D{publicabstractdoublegetCir();publicabstractdoublegteSq();}classCircleextendsShape2D{privatestaticfinaldoublePZ=3>14;doubler;publicCircle(inti){r=i;}publicdoublegetR(){returnr;}publicvoidsetR(doubler){
3、this.r=r;}publicdoublegetCir(){return2*PI*this.r;}publicdoublegteSq(){returnPI*this•r*this•r;classTritangleextendsShape2D{doublesideA^sideB^sideC^area^length;booleanboo;publicTritangle(doublea^doubleb,doublec){this.sideA=a;this・sideB=b;this.sideC=c;if(((a+b)>c&&(a-b)4、)&&((a+c>b)&&(a-ca)&&(b-c5、.println("不是一个三角形,不能计算而积”);return0;}}}classRectangleextendsShape2D{doublewid;doublelen;publicRectangle(){}publicRectangle(doublewidth,doublelength){this・wid=width;this.len=length;}publicdoublegetCir(){return(wid+len)*2;}publicdoublegteSq(){returnwid*len;}}classSquareex6、tendsRectangle{doublewidth;publicSquare(doublewidth){//super(width,width);this・width=width;//TODOAuto・generatedconstruetorstub}doublewid;publicdoublegetCir(){return4*width;}publicdoublegteSq(){returnwidth*width;}}classTest{publicstaticvoidmain(String[]args){Circlecl=ne7、wCircle(5);System.out.println(”thecircumsfereneeofthecircleis:”);System.out.println(+cl>getCir());System.out.printlnC'thesquaeofthecircleis:”);System.out.println(+cl.gteSq());Tritangletl=newTritangle(7,4,5);System.out.println(壮hecircumsfereneeofthecircleis:"System.out.8、println(+tl>getCir());System.out.println(”thesquaeofthecircleis:”);System•out•printIn(+tl•gteSq());Rectanglerl=newRec
4、)&&((a+c>b)&&(a-ca)&&(b-c5、.println("不是一个三角形,不能计算而积”);return0;}}}classRectangleextendsShape2D{doublewid;doublelen;publicRectangle(){}publicRectangle(doublewidth,doublelength){this・wid=width;this.len=length;}publicdoublegetCir(){return(wid+len)*2;}publicdoublegteSq(){returnwid*len;}}classSquareex6、tendsRectangle{doublewidth;publicSquare(doublewidth){//super(width,width);this・width=width;//TODOAuto・generatedconstruetorstub}doublewid;publicdoublegetCir(){return4*width;}publicdoublegteSq(){returnwidth*width;}}classTest{publicstaticvoidmain(String[]args){Circlecl=ne7、wCircle(5);System.out.println(”thecircumsfereneeofthecircleis:”);System.out.println(+cl>getCir());System.out.printlnC'thesquaeofthecircleis:”);System.out.println(+cl.gteSq());Tritangletl=newTritangle(7,4,5);System.out.println(壮hecircumsfereneeofthecircleis:"System.out.8、println(+tl>getCir());System.out.println(”thesquaeofthecircleis:”);System•out•printIn(+tl•gteSq());Rectanglerl=newRec
5、.println("不是一个三角形,不能计算而积”);return0;}}}classRectangleextendsShape2D{doublewid;doublelen;publicRectangle(){}publicRectangle(doublewidth,doublelength){this・wid=width;this.len=length;}publicdoublegetCir(){return(wid+len)*2;}publicdoublegteSq(){returnwid*len;}}classSquareex
6、tendsRectangle{doublewidth;publicSquare(doublewidth){//super(width,width);this・width=width;//TODOAuto・generatedconstruetorstub}doublewid;publicdoublegetCir(){return4*width;}publicdoublegteSq(){returnwidth*width;}}classTest{publicstaticvoidmain(String[]args){Circlecl=ne
7、wCircle(5);System.out.println(”thecircumsfereneeofthecircleis:”);System.out.println(+cl>getCir());System.out.printlnC'thesquaeofthecircleis:”);System.out.println(+cl.gteSq());Tritangletl=newTritangle(7,4,5);System.out.println(壮hecircumsfereneeofthecircleis:"System.out.
8、println(+tl>getCir());System.out.println(”thesquaeofthecircleis:”);System•out•printIn(+tl•gteSq());Rectanglerl=newRec
此文档下载收益归作者所有