《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