如何通过父类的方法调用子类的方法?

huangapple go评论58阅读模式
英文:

How to call the method of the subclass using method of Parent class?

问题

public class Shape {

    double width, height;

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

    public void area(Shape shape) { // area method of parent class
        shape.calculateArea(); // here I am getting an error.
    }

    public void calculateArea() {
        // To be implemented by subclasses
    }
}

class Triangle extends Shape {

    public Triangle(double w, double h) {
        super(w, h);
    }

    @Override
    public void calculateArea() // area method of derived class
    {
        double area = (1.0 / 2.0) * width * height;
        System.out.println("The area of triangle is: " + area);
    }
}

class Rectangle extends Shape {

    public Rectangle(double w, double h) {
        super(w, h);
    }

    @Override
    public void calculateArea() // area method of derived class
    {
        double area = width * height;
        System.out.println("The area of rectangle is: " + area);
    }
}

请注意,我在代码中进行了一些修改:

  1. 将类名 "triangle" 改为 "Triangle",类名应该以大写字母开头,符合 Java 的命名规范。
  2. 将类名 "rectangle" 改为 "Rectangle",同样的原因,类名应以大写字母开头。
  3. 将方法名 "area" 改为 "calculateArea",以避免与父类的 "area" 方法名称冲突。
  4. 在父类 "Shape" 中添加了一个名为 "calculateArea" 的抽象方法,让子类来实现自己的具体面积计算逻辑。
  5. 子类 "Triangle" 和 "Rectangle" 分别重写了父类的 "calculateArea" 方法,实现了各自的面积计算逻辑。

这些修改可以帮助您实现正确的面积计算并调用。

英文:

I have a Shape method that has two arguments first is the width and the second is the height. I have two subclasses, one is the rectangle and another is triangle. I want to call the area()
the method defined in both triangle and rectangle with the help of the area() of the Shape class.
I have written this piece of code but when I am calling the area() method of derived classes using the area() method of the parent class, I am getting an error.
So how to do this?

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);
    }
}

答案1

得分: 2

你希望覆盖这个方法,让子类来实现它。你根本不需要调用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(); // 调用 Triangle.area()

shape = new Rectangle(50f, 50f);
float areaOfQuad = shape.area(); // 调用 Rectangle.area()
英文:

You want to override the method and let the subclasses implement it. You do not need to call any method from Shape.area() at all!

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; }
}

With that in place, you can do:

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()

答案2

得分: 1

正如@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{

    public Triangle(double w, double h) {
        super(w, h);
    }

    public void area()// 派生类的面积方法
    {
        double area = (1/2)*this.width*this.height;
        System.out.println("三角形的面积是:" + area);
    }
}

class Rectangle extends Shape{

    public Rectangle(double w, double h) {
        super(w, h);
    }
    
    public void area() // 派生类的面积方法
    {
        double area = this.width*this.height;
        System.out.println("矩形的面积是:" + area);
    }
}

解释:
父类中的abstract关键字用于当您希望扩展此类的所有类都实现某个方法(在本例中为area()方法),但实现可能不同的情况下。如果在扩展Shape的类中不实现area()方法,将会出错。

如三角形类中所注释的,您不需要在对象内部保存对对象本身的引用。对于这种用例,您可以使用this。例如this.area()或this.width。

最后但同样重要的是,您可以将面积方法更改为返回面积的double值。这不是问题,但如果以后您想进一步处理该值,可能会对您有所帮助。

英文:

As @Turing85 pointed out abstract methods and classes are the thing you are looking for.
If you want to understand the concept there are a lot of helpful articles out on the internet.

What would help you is the following:

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);
    }
}

Explanation:
The abstract keyword in the parent class is used when you want all classes that extend this class to implement a certain method - in this case the area() method - but the implementations differ. If you don't implement area() in a class that extends Shape, you will get an error.

As commented in the triangle class you don't need to save a reference to the object itself within the object. For this use case, you would use this. E.g. this.area() or this.width.

And last but not least, you could change your area method to return the double value of the area. That is not the problem but could later help you if you want to process that value further.

huangapple
  • 本文由 发表于 2020年9月18日 18:33:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/63954066.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定