【问题标题】:Polymorphism is not working in my Java code [duplicate]多态在我的 Java 代码中不起作用 [重复]
【发布时间】:2021-04-14 11:23:20
【问题描述】:

我是 Java 的初学者,正在尝试学习 Java 中的多态性。我试着做一个简单的例子,但我的代码表现得很奇怪。所以这就是问题所在。我正在我的主要功能中创建一个圆圈,它来自 Shape。我的意思是;

Shape sc = new Circle();

当我尝试访问 Circle 类函数之一时,在该行之后,我无法在我的自动代码完成中看到该函数。我的意思是;

sc.getArea() //is not working

如果有人可以帮助我,我将不胜感激......

这是我的 Circle 类:

public class Circle extends Shape {
    public double radius;

    public Circle() {
        this.radius = 1.0;
    }

    public Circle(double radius) {
        this.radius = radius;
    }

    public Circle(String color, boolean filled, double radius) {
        super(color, filled);
        this.radius = radius;
    }

    public double getRadius() {
        return radius;
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }

    public double getArea() {
        double area;
        area = PI * radius * radius;

        return area;
    }

    public double getPerimeter() {
        double perimeter;
        perimeter = (2*radius)*PI;

        return perimeter;
    }

    @Override
    public String toString() {
        return "A Circle with radius "+this.radius+", which is a subclass of Shape";
    }
}

这是我的形状类:

public class Shape {
    public String color;
    public boolean filled;
    public static final double PI = 3.14;

    public Shape(String color, boolean filled) {
        this.color = color;
        this.filled = filled;
    }

    public Shape(){
        this.color = "Green";
        this.filled = true;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public boolean isFilled() {
        return filled;
    }

    public void setFilled(boolean filled) {
        this.filled = filled;
    }

    @Override
    public String toString() {
        if(this.filled){
            return "A Shape with color of " + this.color +" and filled";
        }
        else
            return "A Shape with color of " + this.color +" and not filled";

    }
}

这是我的主要功能:

public class ShapeMain {
    public static void main(String[] args) {
        Circle circle = new Circle(2.0);
        Square square = new Square(2.0);
        Rectangle rectangle = new Rectangle(2.0,3.0);

        Shape sc = new Circle(2.0);
        Shape ss = new Square(2.0);
        Shape sr = new Rectangle(2.0,3.0);

        // sc.getArea() ... is not working
        //Polymorphisim is not working

    }
}

【问题讨论】:

  • 我猜你的问题不在于多态性。在尝试理解多态性之前,尝试理解什么是继承,方法和属性是如何被继承的。另外,看看访问修饰符。
  • Shape 没有getArea 方法,而scShape,那么为什么您的IDE 会建议一个不属于该类的方法呢?

标签: java class oop polymorphism


【解决方案1】:

Shape 没有名为getArea() 的方法,因此编译器不允许您调用它。

要查看多态性,一种简单的方法是定义Shape abstract 并向其添加abstract getArea() 方法:

public abstract class Shape {
  // ... all the stuff you already have

  public abstract double getArea();
}

您可以简单地将其余代码保持不变,现在您可以对Shape 类型的任何变量调用getArea()

当然,每个扩展Shape 的非abstract 类现在都必须为getArea() 方法提供实际实现(即代码)。

【讨论】:

    【解决方案2】:

    您必须强制转换 sc 对象才能访问 Circle 类的功能:

    Shape sc = new Circle();
    Circle circle = (Circle) sc;
    circle.getArea();
    

    因此,您使用的是同一个对象(不是实例化新对象),但以不同的方式表示:首先是 Shape,然后是 Circle。

    强制转换不会丢失任何属性值。

    【讨论】:

      【解决方案3】:

      你很亲密。这是您在示例中需要执行的操作: 由于变量是一个Shape,你只能要求它做一个Shape可以做的事情。 所以,如果你想问它getArea,Shape必须有一个方法getArea。 如果将 Shape 的 getArea 方法抽象化,Java 会将调用重定向到 Circle 的 getArea。

      【讨论】:

        猜你喜欢
        • 2018-12-09
        • 2023-03-06
        • 2018-05-29
        • 2012-11-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-20
        相关资源
        最近更新 更多