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

渐近线

我试图为我的任务创建一个二维形状。为此,我将首先创建一个抽象的 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();
        }
        
    
    
    }
MC帝王

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

但是,在方法的主体内,您可以将类型转换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将发出编译错误。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

对子类实例使用超类类型

Java在超类方法中返回不带通用参数的子类

如何在超类方法中使用子类参数?

对子类对象的超类引用显示与对子类对象的子类引用相同的行为

超类和子类——方法使用

子类中的参数化超类方法和非参数化(混凝土类型)方法

从超类调用子类方法

子类和超类方法

将子类传递给方法,但将超类作为参数?

C ++-使指向超类的指针与函数中的子类参数匹配

在子类python3中获取超类输入参数

子类中的方法可以重载超类中的方法吗?

如何从修饰的子类方法中调用重写的超类方法

在子类的方法中本地调用超类方法时,使用“ super”关键字还是使用超类实例?

如何使用工厂子类中的子类覆盖超类工厂中的抽象类?

通过传递子类的实例来调用超类的方法,期望子类实例上的超类参数

Java:从超类构造函数中的子类调用的方法

确定用于调用子类中的方法的超类引用

您如何在Java中从超类调用子类方法?

为什么从子类的超类调用方法中获得“ this”?

Java:在超类方法签名中返回子类

是否可以从子类的超类中取代方法?

从超类对象数组中调用子类方法

超类引用无法在Java中调用子类方法

在向量C ++中从超类调用子类方法

如何让子类扩展python中超类的方法

Java继承:在超类中调用子类方法

如何在Groovy中从静态超类方法引用子类

如何访问子类中超类的受保护方法?