欢迎来到天天文库
浏览记录
ID:15398176
大小:291.00 KB
页数:3页
时间:2018-08-03
《java抽象类和接口类例子》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、Java作业抽象类:已知电流和电阻,计算电压和电功率。1、packageJavawork;publicabstractclassPhysical{publicabstractdoublefn1();publicabstractdoublefn2();}2、packageJavawork;publicclassPhysicalEquationextendsPhysical{publicdoubleI;//电流publicdoubleR;//电阻publicPhysicalEquation(doublea,doubleb){I=a;R=b;}@Over
2、ridepublicdoublefn1(){returnI*R;}@Overridepublicdoublefn2(){returnI*I*R;}}3、packageJavawork;publicclassAppPyhsical{/***@paramargs*/publicstaticvoidmain(String[]args){PhysicalobjP;PhysicalEquationobjPH=newPhysicalEquation(2,3.5);objP=objPH;System.out.println("电压:"+objP.fn1())
3、;System.out.println("电功率:"+objP.fn2());}}接口类:年龄。1、packageJavawork;publicinterfacePerson{publicdoubleAge();}2、packageJavawork;publicclassAgeimplementsPerson{doublea;publicchar[]Age;publicAge(){};publicdoubleAge(){return18;}}3、packageJavawork;publicclassAppPerson{/***@paramarg
4、s*/publicstaticvoidmain(String[]args){PersonobjP;AgeobjA=newAge();objP=objA;System.out.println("年龄:");System.out.println(+objP.Age());}}
此文档下载收益归作者所有