对子类中的子类方法使用超类参数

时间:2021-02-26 13:19:56

标签: java oop

我试图为我的任务创建一个二维形状。为此,我将首先创建一个抽象 Shape2d 类。我的分配清楚地表明我需要覆盖对象类中的 equals(Object o) 方法,该方法将比较 2d 形状。我通过比较形状区域和周长不尊重二维平面中的位置进行了比较。但这里的问题是,我不能使用抽象类Shape2d 的抽象方法。由于我需要覆盖方法equals(Objcet o),因此我无法转换为equals(Shape2d o)。我可以用那个做什么?下面是我的代码。

/** This class is an abstract class and it is created for to be inherited specialized 2d shapes
     *  @version 02.26.2021
     */
    public abstract class Shape2d 
    {
        private int xLocation;
        private int yLocation;
    
        public Shape2d(int xLocation, int yLocation)
        {
            this.xLocation = xLocation;
            this.yLocation = yLocation;
        }
    
        /** Getting the yLocation
    
         *  @version 02.26.2021
         *  @return
         */
        public int getYLocation()
        {
            return yLocation;
        }
    
        /** Getting the xLocation
    
         *  @version 02.26.2021
         *  @return
         */
        public int getXLocation()
        {
            return xLocation;
        }
    
        /** Abstact method which will be used for other 2d objects. Calculating the area of the 2d shape
    
         *  @version 02.26.2021
         *  @return
         */
        public abstract double calculateArea();
    
        /** Abstact method which will be used for other 2d objects. Calculating the perimeter of the 2d shape 
    
         *  @version 02.26.2021
         *  @return
         */
        public abstract double calculatePerimeter();
    
        /** This method will calculate the distance of the two objects in absolute values.
    
         *  @param anyShape
         *  @return
         */
        public double calculatingDistance(Shape2d anyShape)
        {
            if(anyShape instanceof Shape2d)
            {
                return Math.sqrt(Math.abs(Math.pow(yLocation - anyShape.yLocation,2) + Math.pow(xLocation - anyShape.xLocation, 2)));
            }
    
            return -1;    
        }
    
        @Override
        /** This method is Overrided. It will return the Cordinates of the center (x,y)
    
         *  @version 02.26.2021
         */
        public String toString()
        {
            return "Center Cordinates of the Shape is: (" + xLocation +"," + yLocation + ")\n";
        }
    
    
        /** This method is overriding the equals method for finding out whether or not this shapes are the same
         *  disrespect to its position in the 
    
         *  @version 02.26.2021
         *  @param anyShape
         *  @return
         */
        public boolean equals(Shape2d anyShape)
        {
            return calculateArea() == anyShape.calculateArea() && calculatePerimeter() == anyShape.calculatePerimeter();
        }
        
    
    
    }

1 个答案:

答案 0 :(得分:1)

equals 接受 Object。因此您必须使用public boolean equals(Object o)。参数类型是方法签名的一部分。如果您使用其他参数类型(Shape2d 而不是 Object),那么它是一个方法重载而不是一个覆盖

但是,在方法的主体内,您可以类型转换Shape2d 的实例。当然,我们首先要检查 o 是否实际上是 Shape2d 的实例,使用 instanceof

if (o instanceof Shape2d) {
    Shape2d s = (Shape2d) o;
    // Do something with s
}

请注意,许多 equals 实现使用 this.getClass() == o.getClass()

正如评论中已经提到的,编译器只允许强制转换为与受强制转换的类型兼容的类。

  • 向上转型总是有效的,但(几乎)从来没有必要。例如,Object o = (Object) someStringInstance
  • 对自身的强制转换总是有效的,但从来没有必要。例如,String s = (String) someStringInstance
  • 如果目标类型实际上是主题类型的子类型,则向下转换(即转换为更具体的类型)始终有效。
  • 如果源类型和目标类型不相关,那么编译器将不允许这样做,并发出编译错误。例如,LocalTime time = (LocalTime) someStringInstance 将发出编译错误。