暴力野牛 2021-04-14 16:13 采纳率: 100%
浏览 21
已采纳

不能理解的java代码“Triangle(Triangle ob) { super(ob); }”

下面是原代码

abstract class TwoDShape { // 定义抽象类
    private double width;
    private double height;
    private String name;

    TwoDShape() {
        width = height = 0.0;
        name = "null";
    }

    TwoDShape(double w, double h, String n) {
        width = w;
        height = h;
        name = n;
    }

    TwoDShape(double x, String n) {
        width = height = x;
        name = n;
    }

    TwoDShape(TwoDShape ob) {
        width = ob.width;
        height = ob.height;
        name = ob.name;
    }

    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);}
    abstract double area();

} // end class TwoDShape

class Triangle extends TwoDShape { // Triangel 是抽象类 TwoDShape的子类
    private String style;

    Triangle() {
        super();
        style = "null";
    }

    Triangle(String s, double w, double h) {
        super(w, h, "triangle");
        style = s;
    }

    Triangle(double x) {
        super(x, "triangle");
        style = "isosceles";
    }

    Triangle(Triangle ob) {
        super(ob);
        style = ob.style;
    }

    double area() {
        return getWidth() * getHeight() / 2;
    }

    void showStyle() {
        System.out.println("Trangle is " + style);
    }
} // end class TwoDShape

class Rectangle extends TwoDShape { // Rectangle是抽象类的子类
    Rectangle() {
        super();
    }

    Rectangle(double w, double h) {
        super(w, h, "retangle");
    }

    Rectangle(double x) {
        super(x, "rectangle");
    }

    Rectangle(Rectangle ob) {
        super(ob);
    }

    boolean isSquare() {
        if (getWidth() == getHeight())
            return true;
        else
            return false;
    }

    double area() {
        return getWidth() * getHeight();
    }
} // end class Rectangle

public class AbstractDemo {
    public static void main(String [ ] args) {
        TwoDShape shapes[ ] =new TwoDShape[4]; // ? shapes[ ]
             shapes[0] = new Triangle("right",8.0,12.0);
             shapes[1] = new Rectangle(10);
             shapes[2] = new Rectangle(10,4);
             shapes[3] = new Triangle(7.0);
             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();
                     }
    }
} // end class AbstractDemo
疑问:ob不应该是引用Triangle的对象吗Triangle(Triangle ob) { super(ob); },为什么又在这TwoDShape(TwoDShape ob)

是上转型还是什么呢

  • 写回答

4条回答 默认 最新

  • 关注

    子类可以直接转换为父类,只要是TwoDShape类的子类传进来的对象都可以接收。抽象类是介于接口和类之间的,这样定义的目的就是可以让TwoDShape类的任何子类都可以当做参数传进来初始化父类,而不用定义多个方法。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助