Java中类的继承重写.doc

Java中类的继承重写.doc

ID:62033706

大小:41.00 KB

页数:7页

时间:2021-04-15

Java中类的继承重写.doc_第1页
Java中类的继承重写.doc_第2页
Java中类的继承重写.doc_第3页
Java中类的继承重写.doc_第4页
Java中类的继承重写.doc_第5页
资源描述:

《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)

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。