多态性

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

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

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值