欢迎来到天天文库
浏览记录
ID:62033706
大小:41.00 KB
页数:7页
时间:2021-04-15
《Java中类的继承重写.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、1.自编案例总结包的创建和导入/**包的创建*/packagecom.by3g.test;importjava.util。Scanner;/* *此类实现通过:调用有参方法和无参方法(function)求出三角形的面积。*/class Area{publicdoublewidth,height,area;Scannerscanf=newScanner(System。in);/* *功能实现,要求输入并求出三角形的面积(也是重载) */ publicvoidfunction(){System.out.print(”请输入三角行的长度,宽度:");wi
2、dth=scanf.nextDouble();height=scanf.nextDouble();System.out.println("三角形的面积为:"+(width*height/2)+"平方米");} /*Sss*类的功能方法实现,要求传参数过来(重载)*/publicdouble function(doublewidth,double height){area=width*height/2;returnarea; }}//Create类继承Area的所有方法和属性public classCreate extendsArea{}/**包的导
3、入*/import com.by3g.test.Create;/**共类继承Create类 */publicclassInputextends Create{public staticvoidmain(String[]args){Inputa=newInput();//Area中无参重载调用 a.function();//Area中有参重载调用System.out.println("自带参数的三角形面积为:"+a。function(100.2,122)+”平方米"); }}运行结果:2。自编案例总结类的单继承(包括属性和方法的继承)/**建筑类
4、是父类*/classBuilding{doublewidth,height,length,area; Building(){}Building(doublewidth,double height,doublelength){this.width=width; this。height=height; this.length=length; }void function(){area=width*length;System.out。println(”此建筑物的占地面积为:"+area+"平方米高度为:"+height);}}class Villa ex
5、tendsBuilding{Stringname;Villa(){} Villa(doublewidth,doubleheight,double length) {super(width,height,length);}voidfunctionVilla(){ area=width*length; System。out。println("\n此别墅:"+name+”占地:"+area+”平方米高度为:"+height);}}classHost{publicstaticvoidmain(String[]args){Buildingb;b=newVil
6、la(120,100,100.35);b.function(); Villav=newVilla();v.width=100; v.height=50; v。length=120; v.name=”漫步云端”; v。functionVilla();}}运行结果:3.自编案例总结super的两种用法:super()和super.父类方法()classParent{ doublewidth,height,depth;Parent(){System。out.println("调用Parent构造函数!");}Parent(doublewidth,doub
7、leheight,doubledepth){this.width=width;this。height=height;this.depth=depth;}voidfunction(){System.out.println("三者之积为:"+height*width*depth);}void function1(doublea){System.out.println("zzzzzzzz");}} classSubextendsParent{Sub(){ System。out.println("调用Sub中的无参构造函数!”); }Sub(double
8、 width,doubleheight,doubledepth) { super(width,height,depth)
此文档下载收益归作者所有