欢迎来到天天文库
浏览记录
ID:62033562
大小:278.50 KB
页数:3页
时间:2021-04-15
《Java抽象类和接口类例子.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、Java作业抽象类:已知电流和电阻,计算电压和电功率.1、packageJavawork;public abstractclassPhysical{public abstract double fn1();ﻩpublicabstract doublefn2();}2、packageJavawork;publicclassPhysicalEquation extendsPhysical {publicdoubleI;//电流publicdoubleR;//电阻ﻩpublic PhysicalEquation(doubl
2、ea,double b){ﻩﻩI=a;ﻩﻩR=b;}ﻩ@Overrideﻩpublicdouble fn1(){returnI*R;ﻩﻩ}@Overrideﻩpublic double fn2(){returnI*I*R;ﻩﻩ}}3、packageJavawork;publicclassAppPyhsical{ﻩ/**ﻩ * @paramargs*/ﻩpublicstaticvoid main(String[] args){ﻩﻩPhysicalobjP;ﻩPhysicalEquation objPH=newPhysi
3、calEquation(2,3.5);ﻩobjP=objPH;System.out.println("电压:\n"+objP.fn1());System.out.println(”电功率:"+objP。fn2());}}接口类:年龄。1、packageJavawork;publicinterfacePerson{publicdoubleAge();}2、packageJavawork;public classAgeimplements Person{ ﻩdoublea;publicchar[] Age;publi
4、c Age(){};publicdoubleAge(){ﻩreturn18; } }3、packageJavawork;publicclassAppPerson{ﻩ/**ﻩ*@paramargs */publicstatic void main(String[]args){ﻩPersonobjP;ﻩAgeobjA=newAge();ﻩﻩobjP=objA;System。out.println(”年龄:”);ﻩSystem.out。println(+objP.Age());}}
此文档下载收益归作者所有