Java程序设计Java程序设计 (14).pdf

上传人:刘静 文档编号:69163605 上传时间:2022-12-30 格式:PDF 页数:66 大小:1.95MB
返回 下载 相关 举报
Java程序设计Java程序设计 (14).pdf_第1页
第1页 / 共66页
Java程序设计Java程序设计 (14).pdf_第2页
第2页 / 共66页
点击查看更多>>
资源描述

《Java程序设计Java程序设计 (14).pdf》由会员分享,可在线阅读,更多相关《Java程序设计Java程序设计 (14).pdf(66页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、4extendsObjectsuperprotectedfinal+extends()-extendsextendsclass extends/class Circle extends Shape/(is-a)is-aCircle is-a ShapeRectangle is-a Shape1.2.CircleCircle c=new Circle(10);c.getId();c.getRadius();1.2.3.private4.privateprivate5.is-a6.JavaShape-toStringequalsObject-ObjectObjecttoStringequalsOb

2、jectObject-ObjectObjectObjectString toString()/boolean equals()/int hashCode()/Object clone()/Class getClass()/ClasstoStringtoString()System.out.println()toStringSystem.out.println(new Circle();equalsequals()(is-a)ObjectObjecttoStringequalsObjectPersonsysoutPersonString str1=new String(123);String s

3、tr2=new String(123);System.out.println(str1.equals(str2);Stringequals(Override)(+)StringequalsStringequlasObject(Override)1.PersontoStringname age2.Personequalsnameagetruefalse-ShapeCircleRectangleShapepublicgetArea()getPerimeter()CircleRectangle-publicprivatepublic Object test();public String test(

4、);-OverrideEclipseObjecttoStringhashCodeequalsOverload vs OverrideMath.max(float a,float b)Math.max(int a,int b)StringtoStringPersonequals()supersupersupersuper.super.superthissupersupersuper()1.2.super()3.-1.SonObjectFather2.3.super4.Object-superclass A int i;void value()i=10;System.out.println(i);

5、public class B extends A int i;void value()i=20;super.value();/输出什么System.out.println(i);/i?System.out.println(super.i);/i?-ParentChildParent is initializing!Child is initializing!mainChildParentParent(String name)-is-a Shape cir=new Circle(10.0);Shape rect=new Rectangle(10.0,10.0);cirrectis-a Shape

6、cir.getRadius()ObjectObject x=new Circle(10.0);ObjectShapegetArea()Shape cir=new Circle(10.0);Shape rect=new Rectangle(10.0,10.0);cir.getArea();/rect.getArea();/Dynamic BindingJVM:1.2.Circle cirsCircleRectangle rectsRect1():double sum(Circle cirs)double sum(Rectangle rects)sum(cirs)+sum(rects)1()Squ

7、are:forgetArea()CircleRectangleSquareShapeis-a ShapegetArea()getArea()2()public static double sum(Shape shapes)double area=0;for(Shape e:shapes)area+=e.getArea();/return area;Shape s=new Shape3;s0=new Circle;s1=new Rectangle;s2=new Square;double area=sum(s);(dynamic binding)()1.hashCode2.Shape3.doub

8、le getRadius(Shape s)s:Shape s=new Circle(10.0);Circle c=s;/ShapeCircleShapesCircleCirclegetRadius()Circle c=(Circle)s;c.getRadius();Shape s=new Rectangle(10.0,10.0);Circle c=(Circle)s;/java.lang.ClassCastExceptionRectangle cannot be cast to Circleinstanceoftruefalse:ShapeCircleinstanceofObjectnullS

9、tringpublicprivateprotected()public()BaseOtherone()publicprivateprotectedpublicYYYYprotectedYYYNYYNNprivateYNNN1.private2.public3.public4.protected()5.final-finalfinal double PI=3.14;final(immutable)String(IntegerDouble)finalfinalObjectfinalStringShapefinal-abstractabstractpublic abstract class Shap

10、eShapeabstractpublic abstract double getArea();ShapegetArea()-abstractShapeCircleRectangleabstractabstractShapeCircleRectangleabstractshapes-1.getAreaShape2.protectedprotectedprotectedObjectcloneprotectedX(Object)cloneXclonepublic-3.is-a:LaserWeaponvoid fire()destory what you see!;SuperManfire()Supe

11、rManLaserWeapon-4.ObjectequalsCircleeqaulsgetAreapublic boolean equals(Object obj)return this!=obj;public double getArea()return 0.0;-5.,if(x instanceof Circle)getCircleArea(x)else if(x instanceof Rectangle)getRectangleArea(x)public double sumAllArea(Shape shapes)double sum=0;for(Shape shape:shapes)

12、sum+=shape.getArea();return sum;-6.abstractabstract class Shapepublic abstract double getArea();EmployeeManagerEmployeeManagerEmployeebonusgetBonussupergetSalarysupermainforJava1Cay S.HorstmannGary Cornell ManagerTest.javaPersonTest.javaUMLUML+public,-private,:ShapeUtilShape5UMLUMLEclipsealt+shift+stoStringhashCodeequalstoStringhashCodeequalsctrl+o

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 大学资料

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁