Java中的函数重载(override)

//Use dynamic method dispath
class TwoDShape{
    private double width;
    private double height;
    private String name;
    
    //A default constructor
    TwoDShape(){
        width = height = 0.0;
        name = "none";
    }
    
    //Parameterized constructor
    TwoDShape(double w, double h, String n){
        width = w;
        height = h;
        name = n;
    }
    
    //Constructor boject with equal width and height
    TwoDShape(double x, String n){
        width = height = x;
        name = n;
    }
    
    //Construct an object from an object
    TwoDShape(TwoDShape ob){
        width = ob.width;
        height = ob.height;
        name = ob.name;
    }
    
    //Accessor methods for width and height
    double getWidth(){return width;}
    double getHeight(){return height;}
    void setWidth(double w){width = w;}
    void setHeight(double h){height = h;}
    
    String getName(){return name;}
    
    void showDim(){
        System.out.println("Width and height are " + width + " and " + height);
    }
    
    double area(){
        System.out.println("area() must be overridden");
        return 0.0;
    }
}

//A subclass of TwoDShape for triangles.
class Triangle extends TwoDShape{
    private String style;
    
    //A default constructor
    Triangle(){
        super();
        style = "none";
    }
    
    //constructor for Triangle
    Triangle(String s, double w, double h){
        super(w, h, "triangle");
        style = s;
    }
    
    //One argument constructor
    Triangle(double x){
        super(x, "triangle"); //call superclass constructor
        style = "filled";
    }
    
    //constructor an object from an object
    Triangle(Triangle ob){
        super(ob);//pass object to TwoDShape constructor
        style = ob.style;
    }
    
    //override area() for Triangle
    double area(){
        return getWidth() * getHeight() / 2;
    }
    
    void showStyle(){
        System.out.println("Triangle is " + style);
    }
}

//A subclass of TwoDShape for rectangles.
class Rectangle extends TwoDShape{
    //A default constructor
    Rectangle(){
        super();
    }
    
    //constructor for Rectangle
    Rectangle(double w, double h){
        super(w, h, "rectangle");
    }
    
    //constructor a square.
    Rectangle(double x){
        super(x, "rectangle");
    }
    
    //Consturct an object from an object
    Rectangle(Rectangle ob){
        super(ob);
    }
    
    boolean isSquare(){
        if(getWidth()==getHeight()) return true;
        return false;
    }
    
    //Override area() for Rectangle
    double area(){
        return getWidth() * getHeight();
    }
}

class DynShapes{
    public static void main(String args[]){
        TwoDShape shapes[] = new TwoDShape[5];
        
        shapes[0] = new Triangle("outlined", 8.0, 12.0);
        shapes[1] = new Rectangle(10);
        shapes[2] = new Rectangle(10, 4);
        shapes[3] = new Triangle(7.0);
        shapes[4] = new TwoDShape(10, 20, "generic");
        
        for(int i = 0; i < shapes.length; i++){
            System.out.println("object is " + shapes[i].getName());
            System.out.println("Area is " + shapes[i].area());
            System.out.println();
        }
    }
}


转载于:https://my.oschina.net/u/241930/blog/541157

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值