您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 冶金工业 > 52Java第04章--1(继承和多态)
JavaJavapenggang@mail.hust.edu.cnJavaJava(())1.Java(Inheritance)2.Java(Polymorphism)3.4.5.6.4.1JavaJava1.Java“”InheritanceRelationship1.Java”“isa”“hasa”PointCircleCirclePointCircleextendsPoint“”CirclePointCirclePointsubclassPointCircledirectsuperclassCircleCylinderCylinderCircleDirectSubclassCircleCylinderPointCylinderIndirectSuperclassCylinderPointIndirectsubclassCylinderCircleDirectRelationshipIndirectRelationship1.JavaJavaC++JavaPointCirclexyCircle1.Java1.Java1.JavaJavainterfacejava.langObjectObjectObject1.JavasuperJavathissupersuper(r,a,b);Circle(double,int,int)SuperSuper(())(())supera.;b..1.Java4.2P183:ReferenceTest.java1.JavapointRefCirclecCylindercyPoint“”incompatibletypespointRefcpointRefppointRef=p;circleRef=(Circle)pointRef;ClassCastException1.JavaC++Java“super();”“super();”14.4P1874.2JavaJava2.Java3Polymorphismpoly“”morphism“”draw()PointCircleCylinderPointdraw()Java——Overloadingint,float,String,printInt(int);printFloat(float);printString(String)println()println(int);println(float);println(String);Java——Overriding“Overloading”OverridingJava——12Java——“”“”4.6P193StaticTest.javaJava——z4.6zinstanceofz4.24.7P194:PolymorphismTest.javaJava——zstaticStringpolymorMethod(Pointp)//zPointshapeArray[0],CircleÆPointshapeArray[1],RectangleÆPointzz“”4.34.3abstractfinalabstractfinalabstractfinalabstractoverride4.8P197:ShapeAbs.java4.3.1abstractimplementsinterface4.3.2finalfinalfinalfinalfinaljava.langStringpublicfinalclassStringfinal—hacker4.3.31staticprivateabstract2abstract34finalabstract5abstractclassShape{abstractfloatarea();}classRectangleextendsShape{publicfloatwidth,height;Rectangle(floatw,floath){width=w;height=h;}publicfloatarea(){returnwidth*height;}}classCircleextendsShape{publicfloatr;Circle(floatr){this.r=r;}publicfloatarea(){return3.14*r*r;}}4.4Javainterface999Java“”VolumeControl“”ToneControlVolumeControlToneControl“”Java[public]interfaceinterfaceName[extendsSuperInterfaceList]{//[public][static][final]=;//[public][abstract];}interfaceclassclasspublic.javaJava9(1)publicpublic9(2)public9(3)publicstaticfinal3publicabstract9(4)implementsclassimplements[extends]{...finalabstract}2abstractfinal“”implementsimplementsextends4.5JavaJavaJavaJDK1.1classJDK1.1“”nestedclass“”outerclass“”enclosingclass“memberclass”class1Why.4.11P207InnerTest.javaOuterOneouterOuterOne.FriendInnersou=outer.sour(China);thispublicprotectedprivateOuterOnedest()cont()sour()static“”StaticInnerClass4.12P210:TestBed.javapublicclassTestBed{privateintx;staticinty=12;//yTestBed(inta){x=a;}intxValue(){returnx;}publicstaticclassTester{//TesterintyValue(){returny;}////}publicstaticvoidmain(String[]args){Testertt=newTester();TestBedtb=newTestBed(4374);System.out.println((1)tt.y=+tt.yValue());System.out.println((2)tb.x=+tb.xValue());}//main}//}JavaTestBedJavaTestBed$Tester2“”Enclosingmethodstaticstaticfinal4.13P211InnerOfMethodTest.java2newYyy(){}classXxxextendsYyy{}newXxx()//XxxYyyreturnreturnnewYyy(){}function(newYyy(){…});(Yyy)4.14P213:AnonymousTest1.java4.15P2154.61.classPoint{...}classCircleextendsPoint{...}...Pointp=newPoint();Circlec=newCircle();p=c;CirclecPointppOOP“”upstaticintcompare(Pointa,Pointb){…}main{…if(compare(c1,c2)0)…}1Point2“”incompatibletypes3precpCirclecc=(Circle)p;ClassCastException1Java23JavaJava
本文标题:52Java第04章--1(继承和多态)
链接地址:https://www.777doc.com/doc-4876159 .html