【问题标题】:How to call the method of the subclass using method of Parent class?如何使用父类的方法调用子类的方法?
【发布时间】:2020-09-18 10:33:39
【问题描述】:

我有一个 Shape 方法,它有两个参数,第一个是宽度,第二个是高度。我有两个子类,一个是矩形,另一个是三角形。我想打电话给 area() 借助 Shape 类的 area() 在三角形和矩形中定义的方法。 我已经编写了这段代码,但是当我使用父类的 area() 方法调用派生类的 area() 方法时,出现错误。 那么如何做到这一点呢?

public class Shape {

    double width, height;

    public Shape(double w, double h)
    {
        this.height = h;
        this.width = w;
    }
    public void area(Object shape){ // area method of parent class
       shape.area(); // here I am getting error.
    }
}
class triangle extends Shape{

    triangle tri;
    public triangle(double w, double h) {
        super(w, h);
    }
    public void area()// area method of derived class
    {
        double area = (1/2)*width*height;
        System.out.println("The area of triangle is: "+area);
    }
}

class rectangle extends Shape{

    rectangle rect;
    public rectangle(double w, double h) {
        super(w, h);
    }
    public void area() // area method of derived class
    {
        double area = width*height;
        System.out.println("The area of rectangle is: "+area);
    }
}

【问题讨论】:

  • 您很可能正在寻找abstract methods and classes。 --- 备注:奇怪的是方法area(...)期望Shape作为参数而不是使用this来计算并返回当前Shape的面积。
  • 建议您以大写字母开头类名,以便人们更容易阅读您的代码。
  • @matt 不要这样做。如果必须重写一个方法,则该方法(以及周围的类)应声明为abstract
  • 您应该阅读继承的基础知识。
  • 请注意,(1/2) 将是 0。

标签: java oop


【解决方案1】:

您想覆盖该方法并让子类实现它。您根本不需要从Shape.area() 调用任何方法!

public abstract class Shape {
    float width, height;
    Shape(float width, float height) {
       this.width = width;
       this.height = height;
    }
    public abstract float area();
}

public class Rectangle extends Shape {
    public Rectangle(float width, float height) {
       super(width, height);    
    }
    @Override
    public float area() { return width * height; }
}

public class Triangle extends Shape {
    public Triangle(float width, float height) {
        super(width, height);
    }
    @Override
    public float area() { return (width*height) / 2; }
}

有了这些,你可以这样做:

Shape shape = new Triangle(50f, 50f);
float areaOfTri = shape.area(); // dispatches to Triangle.area()

shape = new Rectangle(50f, 50f);
float areaOfQuad = shape.area(); // dispatches to Rectangle.area()

【讨论】:

  • 正确,但我认为使用 int 作为 area 方法的返回值并不是最好的解决方案,对于三角形,这可能会返回不完全正确的值。例如。如果将 5 和 3 作为宽度和高度传递,则当正确结果为 7.5 时,area 方法将返回 7。我建议在这里使用双精度或浮点数。
  • 是的,@MaxHockeborn 先生。你是对的。我将使用 double 代替 int
  • @MaxHockeborn 你是对的,但所需的精度是业务领域的关注点,与手头的问题无关。该原则适用于您需要的任何数据类型(根本不能是原语)。
【解决方案2】:

正如@Turing85 所指出的,抽象方法和类是您正在寻找的东西。 如果您想了解这个概念,互联网上有很多有用的文章。

以下内容对您有帮助:

public class Shape {

    double width, height;

    public Shape(double w, double h)
    {
        this.height = h;
        this.width = w;
    }

    abstract public void area()
}

class triangle extends Shape{

    triangle tri; //Not sure if you need this. If you want to access a object within 
                  //itself use this
    public triangle(double w, double h) {
        super(w, h);
    }

    public void area()// area method of derived class
    {
        double area = (1/2)*this.width*this.height;
        System.out.println("The area of triangle is: "+area);
    }
}

class rectangle extends Shape{

    rectangle rect;
    public rectangle(double w, double h) {
        super(w, h);
    }
    
    public void area() // area method of derived class
    {
        double area = this.width*this.height;
        System.out.println("The area of rectangle is: "+area);
    }
}

说明: 当您希望扩展此类的所有类都实现某个方法(在本例中为 area() 方法)但实现不同时,使用父类中的 abstract 关键字。如果在扩展 Shape 的类中没有实现 area(),会报错。

正如在三角形类中评论的那样,您不需要在对象内保存对对象本身的引用。对于此用例,您将使用它。例如。 this.area() 或 this.width。

最后但并非最不重要的一点是,您可以更改 area 方法以返回该区域的双精度值。这不是问题,但如果您想进一步处理该值,以后可以帮助您。

【讨论】:

    猜你喜欢
    • 2016-02-14
    • 2011-09-04
    • 2017-11-09
    • 2014-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-22
    相关资源
    最近更新 更多