【问题标题】:Java interface method call via object通过对象调用 Java 接口方法
【发布时间】:2013-09-18 03:05:13
【问题描述】:

我的主要方法写如下:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package excercise.pkg5;

/**
 *
 * @author Azraar
 */

    public class TestResizable {

    public static void main(String[] args) {

        Shape obj[] = new Shape[4];
        obj[0] = new Circle(10);
        obj[1] = new Rectangle(10, 20);
        obj[2] = new ResizableCircle(10);
        obj[3] = new ResizableRectangle(10, 20);

        for (int i = 0; i < obj.length; i++) {

            if (obj[i] instanceof ResizableCircle) {
                ResizableCircle r = (ResizableCircle) obj[i];
                obj[i].equals(r);

            }

            if (obj[i] instanceof ResizableRectangle) {
                ResizableRectangle r = (ResizableRectangle) obj[i];
                obj[i].equals(r);

            }

            System.out.println("");
            System.out.print("Object is - " + obj[i].name());
            System.out.print("\nObject Area is - " + obj[i].area());
            System.out.print("\nObject Perimeter is - " + obj[i].perimeter());
        }

    }
}

我正在使用 ResizableRectangle r = (ResizableRectangle) obj[i]; ,因为 resizable 是一个实现,并且 ResizableRectangle 和 ResizableCircle 正在扩展它并覆盖 resize 方法。

如果 instanceof resizbleRectange 或 resizableCircle.. 我需要运行这个 resize() 方法..

obj[i].resize(0.5) 然后它将循环输出并打印。但问题是我没有得到调整大小方法真正的智能感知,当我输入它说找不到符号时......

下面是类层次结构...

编辑:

shape 是根类,您可以在随附的屏幕截图中看到。当对象是 ResizeableClass 和 ResizeableRectangle 的实例时,我正在考虑一种访问 resize 方法的方法。但还是不行。

【问题讨论】:

    标签: java oop interface call implementation


    【解决方案1】:

    您需要在 Resizeable 引用上调用 resize()

    也就是说,如果obj[i]Resizeable,那么你需要先转换然后调用它。仅仅断言obj[i] 引用这样一个对象是不够的,因为编译器只是将其视为基类引用。

    例如

    if (obj[i] instanceof Resizeable) {
       ((Resizeable)obj[i]).resize(..);
    }
    

    【讨论】:

    • 你可以给我举个例子吗?
    • 看上面的一个简单的例子
    • 您不能创建可调整大小的对象。您只能将其用作多态引用。
    • 可调整大小的矩形是可调整大小的。他们有没有教你是 A,并且有 A 关系?
    • @mazraara implements 维护是一个。您可以拥有一个接口的元素数组,即使它们都不是该接口的对象。但是,由于任何子类化它都是接口的 A,因此您可以在对象上使用接口中定义的所有方法。
    【解决方案2】:

    您将obj 声明为

    Shape obj[]
    

    所以obj[i] 将返回一个Shape 对象,我猜它不会扩展Resizeable

    【讨论】:

    • 是的,形状是根类,您可以在随附的屏幕截图中看到。当对象是 ResizeableClass 和 ResizeableRectangle 的实例时,我正在考虑一种访问 resize 方法的方法。
    • 实际上,在屏幕截图中看不到名称Shape :) 无论如何,这就是重点,不能保证Shape 的实例会实现Resizable;即Circle 扩展了Shape,但没有实现Resizable。正如@BrianAgnew 回答的那样,您可以检查obj[i] 确实是Resizable 的一个实例,然后进行转换。
    • 对不起,我是这样拍的。请检查上面编辑过的问题。 Shape 是隐藏屏幕截图中的根类。
    【解决方案3】:

    并非所有 Shape 方法的子类都实现 Resizable 接口。所以你需要检查obj[i]是否是Resizeable类的实例,然后调用resize。

    【讨论】:

      【解决方案4】:

      这终于奏效了……

          if (obj[i] instanceof ResizableCircle) {
              Resizable c = (ResizableCircle) obj[i];
              c.resize(5);
      
          }
      
          if (obj[i] instanceof ResizableRectangle) {
              Resizable r = (ResizableRectangle) obj[i];
              r.resize(10);
      
          }
      

      【讨论】:

        猜你喜欢
        • 2013-08-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多