您好,欢迎访问三七文档
继承与多态classStudent//学生类{Stringnumber;//学号Stringname;//姓名Stringsex;//性别Stringspeciality;//专业public返回值类型method1(参数列表){……}public返回值类型method2(参数列表){……}}classGraduateextendsStudent//研究生类{Stringadviser;//导师public返回值类型method3(参数列表){……}public返回值类型method4(参数列表){……}}classGraduate_EngineeringextendsGraduate//工程硕士类{Stringjob;//工作单位public返回值类型method4(参数列表){……}public返回值类型method5(参数列表){……}}成员变量、方法的继承classInh1{publicvoiddisplay1(){System.out.println(“one”);}}classInh2extendsInh1{publicvoiddisplay2(){System.out.println(“two”);}}publicclassM{publicstaticvoidmain(String[]args){Inh2i2=newInh2();i2.display1();i2.display2();}}classA{inti,j;voidshowij(){System.out.println(i++j);}}classBextendsA{intk;voidshowk(){System.out.println(k);}voidsum(){System.out.println(i+j+k);}}classSimple{publicstaticvoidmain(Stringargs[]){AsuperOb=newA();BsubOb=newB();superOb.showij();subOb.i=7;subOb.j=8;subOb.k=9;subOb.showij();subOb.showk();subOb.sum();}}程序输出结果:0078924关键字this和superclassUsethis{intx=4;//这里x为成员变量voidshow1(){intx=3;//这里x为局部变量System.out.println(x);System.out.println(this.x);}voidshow2(intx){System.out.println(x);this.x=x;System.out.println(this.x);}}classMyclass3{publicstaticvoidmain(Stringargs[]){Usethisob=newUsethis();ob.show1();ob.show2(2);}}classFather{intx=5;//这里x是父类的成员变量publicvoidoutput(){System.out.println(“xofFatheris”+x);}}classSonextendsFather{intx=6;//这里x是子类的成员变量publicvoidoutput(intx)//这里x是子类的局部变量{System.out.println(“xofSonis”+this.x);System.out.println(“xofparameteris”+x);System.out.println(“xofFatheris”+super.x);}publicvoidshow(){super.output();}}classMyclass4{publicstaticvoidmain(Stringargs[]){Fatherobfather=newFather();obfather.output();Sonobson=newSon();obson.output(7);obson.show();}}成员方法的多态一、成员方法重载实现多态为类定义多个名称相同,但参数不同的方法称为方法重载,通过方法重载实现多态。成员方法重载实现多态之一classManyArea{publicdoublearea(doubleradius){returnMath.PI*radius*radius;}publicdoublearea(doublelen,doublewidth){returnlen*width;}publicintarea(intlen,intwidth){returnlen*width;}publicdoublearea(doublelen,doublewidth,doubleheight){returnlen*width*height;}}publicclassMyclass5{publicstaticvoidmain(Stringargs[])ManyAreaob=newManyArea();System.out.println(ob.area(3.0));System.out.println(ob.area(2.0,3.0));System.out.println(ob.area(2,3));System.out.println(ob.area(2.0,3.0,4.0));}}成员方法重载实现多态之二classBasicClass{publicfinalvoidshow(){System.out.println(“show()isfinalmethodofBasicClass”);}publicvoidhello(){System.out.println(“HelloBasicClass”);}}classSubClassextendsBasicClass{publicfinalvoidshow(Strings){System.out.println(s);}publicvoidhello(Strings){System.out.println(“HelloSubClass”);System.out.println(s);}}classMyclass6{publicstaticvoidmain(Stringargs[]){BasicClassob1=newBasicClass();SubClassob2=newSubClass();ob1.show();ob1.hello();ob2.show(“overrideshow()methodofBasicClass”);ob2.hello(“overridehello()methodofBasicClass”);}}二、成员方法覆盖实现多态在子类中重新编写父类的方法称为方法覆盖,通过方法覆盖也可以实现多态。方法覆盖要求子类中定义的方法必须与父类方法具有完全相同的方法头,即方法名、参数列表、返回值都相同。classCleanser{publicvoidshow(){System.out.println(“Runshow()methodofCleanser”);}publicvoidshowsame(){System.out.println(“Runshowsame()method”);}}classDetergentextendsCleanser{publicvoidshow(){System.out.println(“Runshow()methodofDetergent”);super.show();}}classMyclass1{publicstaticvoidmain(Stringargs[]){Cleanserob1=newCleanser();ob1.show();ob1.showsame();Detergentob2=newDetergent();ob2.show();ob2.showsame();}}构造方法的继承和重载构造方法构造方法是类的特殊方法,用于初始化新建对象。其名字与类名相同如果类中没有定义构造方法,默认的构造方法既无参数也无任何操作classApple{Apple(){}//这是构造方法,与类名相同publicdisplay(){System.out.println(“Appleisred”);}}classMyclass8{publicstaticvoidmain(Stringargs[])Applea=newApple();a.display();}}classFatherP92例4.8{Strings=“RunconstructormethodofFather”;publicFather(){System.out.println(s);}}classSonextendsFather{publicvoidoutput(){System.out.println(“Runoutput()methodofSon”);}}classMyclass8{publicstaticvoidmain(Stringargs[])Fatherobfather=newFather();Sonobson=newSon();obson.output();}}classRect{doublea,b;Rect(){a=0.0;b=0.0;}//无参数的构造方法Rect(doublelen,doublewidth){a=len;b=width;}//有参数的构造方法doublearea(){returna*b;}}publicUseRect{publicstaticvoidmain(String[]args){Rectx=newRect();Recty=newRect(2.3,3.0);System.out.println(“x对象的面积是”+x.area());System.out.println(“y对象的面积是”+y.area());}}classArea{doublea=0.0,b=0.0;Area(){}Area(doublewidth){a=width;b=a;}Area(doublelen,doublewidth){a=len;b=width;}Area(doublelen,doublewidth,doubleheight){a=(len+width);b=height/2;}doublecalculateArea(doublea,doubleb){returna*b;}}publicclassMyclass7{publicstaticvoidmain(Stringargs[])Areanot=newArea();Areasquare=newArea(2.0);Arearectangle=newArea(2.0,3.5);Areatrapezoid=newArea(2.5,3.5,4);System.out.println(not.calculateArea());System.out.println(square.calculateArea());System.out.println(rectangle.calculateArea()));System.out.println(trapezoid.calculateArea());}}classFatherP93页例4.9,不指明父类构造方法时,执行无参父类构造方法{Strings=“RunconstructormethodofFather”;publicFather(){System.out.println(s);}publicFather(Stringstr){s=str;System.out.println(s);}}classSonextendsFather{Strings=“RunconstructormethodofSon”;publicSon(){System.out.println(s);}publicSon(Stringstr){this();s=s
本文标题:继承与多态
链接地址:https://www.777doc.com/doc-4060323 .html