为什么java多态在我的例子中不起作用 [英] why java polymorphism not work in my example

查看:29
本文介绍了为什么java多态在我的例子中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这 4 个 Java 类:1

公共类矩形{双倍宽度;双高;字符串颜色;公共矩形(){宽度=0;高度=0;颜色=透明";}公共矩形(双 w,双 h){宽度=w;高度=h;颜色=透明";}双面积(){返回宽度*高度;}}

2

public class PRect extends Rect{双倍深度;公共 PRect(双 w,双 h,双 d){宽度=w;高度=h;深度=d;}双面积(){返回宽度*高度*深度;}}

3

public class CRect extends Rect{字符串颜色;公共 CRect(double w, double h ,String c) {宽度=w;高度=h;颜色=c;}双面积(){返回宽度*高度;}}

4

公共类测试{公共测试(){}公共静态无效主(字符串 [] args){矩形 r1=新矩形(2,3);System.out.println("r1的面积="+r1.area());PRect pr1=new PRect(2,3,4);System.out.println("pr1的面积="+pr1.area());CRect cr1=new CRect(2,3,"RED");System.out.println("cr1的面积="+cr1.area()+" color = "+cr1.color);System.out.println("
 POLY_MORPHISM ");矩形 r2=新矩形(1,2);System.out.println("r2的面积="+r2.area());矩形 pr2=new 矩形(1,2,4);System.out.println("pr2的面积="+pr2.area());矩形 cr2=new CRect(1,2,"蓝色");System.out.println("cr2的面积="+cr2.area()+" color = "+cr2.color);}}

我得到了输出:

<前>r1=6.0的面积pr1=24.0的面积cr1=6.0 的区域颜色=红色POLY_MORPHISMr2=2.0 的面积pr2=8.0的面积cr2的面积=2.0颜色=透明***

为什么将 cr2 视为 Rect(超类)并且具有透明"颜色而不是具有蓝色"颜色的 CRect(子类)?

解决方案

这是使用可见字段的问题之一——你最终会使用它们...

您在 RectCRect 中都有一个 color 字段.字段是不是多态的,所以当你使用 cr2.color 时,它使用在 Rect 中声明的字段,它always 设置为 透明".

您的 CRect 类应该有自己的 color 字段 - 它应该为超类构造函数提供颜色.单个矩形有两个不同的 color 字段是没有意义的 - 当然它可以有 borderColorfillColor - 但只是 color 太含糊了...

I have these 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;
    } 
}

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

3

public class CRect extends Rect{ 
    String color;

    public CRect(double w, double h ,String c) {
        width=w;
        height=h;
        color=c;
    }

    double area()
    {
        return  width*height;
    }     
}

4

public class test {

    public test() { }

    public static void main(String[] args) {  
        Rect r1=new Rect(2,3);
        System.out.println("area of r1="+r1.area());

        PRect pr1=new PRect(2,3,4);
        System.out.println("area of pr1="+pr1.area());


        CRect cr1=new CRect(2,3,"RED");
        System.out.println("area of cr1="+cr1.area()+"  color = "+cr1.color);


        System.out.println("
 POLY_MORPHISM ");
        Rect r2=new Rect(1,2);
        System.out.println("area of r2="+r2.area());

        Rect pr2=new PRect(1,2,4);
        System.out.println("area of pr2="+pr2.area());


        Rect cr2=new CRect(1,2,"Blue");
        System.out.println("area of cr2="+cr2.area()+"  color = "+cr2.color); 

    }
}

I got the output:

area of r1=6.0
area of pr1=24.0
area of cr1=6.0  color = RED
POLY_MORPHISM 
area of r2=2.0
area of pr2=8.0
area of cr2=2.0  color = transparent***

why consider cr2 as Rect(super class) and with "transparent" color not as CRect (sub class) with "Blue" color ?

解决方案

This is one of the problems of using visible fields - you end up using them...

You've got a color field in both Rect and CRect. Fields are not polymorphic, so when you use cr2.color, that uses the field declared in Rect, which is always set to "transparent".

Your CRect class should not have its own color field - it should supply the colour to the superclass constructor. It makes no sense for a single rectangle to have two different color fields - it could have borderColor and fillColor, of course - but just color is too ambiguous...

这篇关于为什么java多态在我的例子中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆