小编eng*_*ngy的帖子

为什么java多态在我的例子中不起作用

我有这4个java clases:1

public class Rect {
    double width;
    double height;
    String color;

    public Rect( ) {
        width=0;
        height=0;
        color="transparent";      
    }

    public Rect( double w,double h) {
        width=w;
        height=h;
        color="transparent";
    }

    double area()
    {
        return  width*height;
    } 
}
Run Code Online (Sandbox Code Playgroud)

2

public class PRect extends Rect{
    double depth;

    public PRect(double w, double h ,double d) {
        width=w;
        height=h;
        depth=d;
    }

    double area()
    {
        return  width*height*depth;
    }     
}
Run Code Online (Sandbox Code Playgroud)

3

public class CRect extends Rect{ 
    String color;

    public CRect(double w, double h ,String c) { …
Run Code Online (Sandbox Code Playgroud)

java polymorphism subclass superclass

6
推荐指数
1
解决办法
1877
查看次数

标签 统计

java ×1

polymorphism ×1

subclass ×1

superclass ×1