多态性

来源:互联网 发布:排行榜java手机游戏 编辑:程序博客网 时间:2024/06/05 08:00

可以声明用于实现一个或者多个接口的类。实现接口的类必须声明该接口的所有方法。

-将子类对象的引用赋给超类类型的变量的做法是安全的,因为子类对象是其超类的对象。然而,该引用只能用于调用超类方法。如果代码通过超类变量来引用子类专用的成员,则编译器将报告错误。观看一下错误代码:

Point point;//超类Circle circle = new Circle(120,12,2.4);//子类point = circle ;point.setX(132);//正确,因为是超类中定义的方法point.setRadius(2.5);//错误,不能调用子类的方法

-试图将超类对象的引用赋给子类类型的变量的做法将产生一个编译错误,观看一下代码:

public class TestDemo {    public static void main(String args[]) {        Point point = new Point(20, 20);//超类        Circle circle;//子类        circle = point;//错误    }}

-抽象类和抽象方法:由于不能继承构造函数,不能将它们声明为抽象方法。

观看以下实现多态性的案例:

//定义超类public abstract class Shape {    public double getArea(){        return 0.0;    }    public double getVolume(){        return 0.0;    }    public abstract String getName();}
//Point 类扩展于Shape类public class Point extends Shape{    private int x;    private int y;    public Point(){}    public Point(int x,int y){        this.x=x;        this.y=y;    }    public void setX(int x){        this.x=x;    }    public void setY(int y){        this.y=y;    }    public int getX(){        return this.x;    }    public int getY(){        return this.y;    }    public String getName(){        return "Point";    }    public String toString(){        return "["+getX()+","+getY()+"]";    }}
//Circle 扩展于Point类public class Circle extends Point{    private double radius;    public Circle(){    }    public Circle(int xValue,int yValue,double r){        super(xValue,yValue);        setRadius(r);    }    public void setRadius(double radius) {        this.radius=(radius<0.0?0.0:radius);    }    public double getRadius() {        return radius;    }    public double getDiameter(){        return 2*radius;    }    public double getCircumference(){        return Math.PI*getDiameter();    }    public double getArea(){        return Math.PI*radius*radius;    }    public String toString(){        return "Center="+super.toString()+"; Radius= " +getRadius();    }}
//Cylinder 类扩展于Circle类import java.util.concurrent.CyclicBarrier;public class Cylinder extends Circle {    private double height;    public Cylinder(){    }    public Cylinder(int x,int y, double radius,double heightValue){        super(x,y,radius);        setHeight(heightValue);    }    public void setHeight(double heightValue){        height=(heightValue<0.0?0.0:heightValue);    }    public double getHeight(){        return height;    }    public double getArea(){        return 2*super.getArea()+getCircumference()*getHeight();    }    public double getVolume(){        return super.getArea()*getHeight();    }    public String getName(){        return "Cylinder";    }    public String toString(){        return super.toString()+"; Height= "+getHeight();    }}
//测试程序import javax.swing.*;import java.text.DecimalFormat;public class TestDemo {    public static void main(String args[]){        DecimalFormat twoDight =  new DecimalFormat("0.00");        Point point = new Point(7,11);        Circle circle = new Circle(22,8,3.5);        Cylinder cylinder = new Cylinder(20,30,3.3,10.75);        String output = point.getName()+":"+ point + circle.getName()+":"+circle+ cylinder.getName()+":"+cylinder;        //**********以下利用了多态性**        Shape a[] = new Shape[3];        a[0]= point;        a[1]= circle;        a[2]= cylinder;        //********//**以下迭代方法**        for(int i = 0;i<a.length;i++){            output+="\n\n"+a[i].getName()+":"+a[i].toString()+"\nArea= "+twoDight.format(a[i].getArea())+"\nVolume= "+twoDight.format(a[i].getVolume());        }        JOptionPane.showMessageDialog(null,output);        System.exit(0);    }}
0 0
原创粉丝点击