Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
多态性在我的Java代码中不起作用_Java_Class_Oop_Polymorphism - Fatal编程技术网

多态性在我的Java代码中不起作用

多态性在我的Java代码中不起作用,java,class,oop,polymorphism,Java,Class,Oop,Polymorphism,我是Java初学者,正在努力学习Java中的多态性。我试图做一个简单的例子,但我的代码行为怪异。这就是问题所在。我在我的主函数中创建了一个圆,它来自于形状。我的意思是, Shape sc = new Circle(); sc.getArea() //is not working 在这一行之后,当我尝试访问一个Circle类函数时,我在自动代码完成中看不到该函数。我的意思是, Shape sc = new Circle(); sc.getArea() //is not working 如果

我是Java初学者,正在努力学习Java中的多态性。我试图做一个简单的例子,但我的代码行为怪异。这就是问题所在。我在我的主函数中创建了一个圆,它来自于形状。我的意思是,

Shape sc = new Circle();
sc.getArea() //is not working
在这一行之后,当我尝试访问一个Circle类函数时,我在自动代码完成中看不到该函数。我的意思是,

Shape sc = new Circle();
sc.getArea() //is not working
如果有人能帮助我,我将不胜感激

这是我的圆圈课:

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()
的方法,因此编译器不允许您调用该方法

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

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

  public abstract double getArea();
}
您只需将其余代码保持不变,现在就可以对任何类型的
Shape
变量调用
getArea()


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

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

Shape sc=新圆();
圆圈=(圆圈)sc;
circle.getArea();
因此,您使用的是同一个对象(您没有实例化新对象),但表示方式不同:首先是形状,然后是圆


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

非常接近。以下是您在示例中需要执行的操作: 由于变量是一个形状,您只能要求它执行形状所能执行的操作。 所以,如果您想让它获取区域,Shape必须有一个方法getArea。
如果将Shape的getArea方法抽象化,Java会将调用重定向到Circle的getArea。

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