1

我有一个Color看起来像这样的基类。该类被设计为不可变的,因此具有final修饰符而没有设置器:

public class Color
{
    public static Color BLACK   = new Color(0, 0, 0);
    public static Color RED = new Color(255, 0, 0);
    //...
    public static Color WHITE   = new Color(255, 255, 255);

    protected final int _r;
    protected final int _g;
    protected final int _b;

    public Color(int r, int b, int g)
    {
        _r = normalize(r);
        _g = normalize(g);
        _b = normalize(b);
    }

    protected Color()
    {

    }

    protected int normalize(int val)
    {
        return val & 0xFF;
    }
    // getters not shown for simplicity
}

派生自这个类的是一个ColorHSL类,除了提供Color类的 getter 之外,它还具有色调、饱和度和亮度。这是事情停止工作的地方。

的构造函数ColorHSL需要做一些计算,然后设置_r_b和的值_g但是必须在进行任何计算之前调用超级构造函数。因此引入了无参数构造函数,允许稍后设置Color()final _r_b和。但是, Java 编译器不接受_g无参数构造函数或设置(第一次,在 的构造函数中)。ColorHSL

有没有办法解决这个问题,还是我必须从、和中删除final修饰符?_r_b_g


编辑:

Color最后,我选择了一个包含 RGB 和 HSL 数据的基本抽象类。基类:

public abstract class Color
{
    public static Color WHITE   = new ColorRGB(255, 255, 255);
    public static Color BLACK   = new ColorRGB(0, 0, 0);
    public static Color RED = new ColorRGB(255, 0, 0);
    public static Color GREEN   = new ColorRGB(0, 255, 0);
    public static Color BLUE    = new ColorRGB(0, 0, 255);
    public static Color YELLOW  = new ColorRGB(255, 255, 0);
    public static Color MAGENTA = new ColorRGB(255, 0, 255);
    public static Color CYAN    = new ColorRGB(0, 255, 255);

    public static final class RGBHelper
    {
        private final int   _r;
        private final int   _g;
        private final int   _b;

        public RGBHelper(int r, int g, int b)
        {
            _r = r & 0xFF;
            _g = g & 0xFF;
            _b = b & 0xFF;
        }

        public int getR()
        {
            return _r;
        }

        public int getG()
        {
            return _g;
        }

        public int getB()
        {
            return _b;
        }
    }

    public final static class HSLHelper
    {
        private final double    _hue;
        private final double    _sat;
        private final double    _lum;

        public HSLHelper(double hue, double sat, double lum)
        {
            //Calculations unimportant to the question - initialises the class
        }

        public double getHue()
        {
            return _hue;
        }

        public double getSat()
        {
            return _sat;
        }

        public double getLum()
        {
            return _lum;
        }
    }

    protected HSLHelper HSLValues   = null;
    protected RGBHelper RGBValues   = null;

    protected static HSLHelper RGBToHSL(RGBHelper rgb)
    {
        //Calculations unimportant to the question
        return new HSLHelper(hue, sat, lum);
    }

    protected static RGBHelper HSLToRGB(HSLHelper hsl)
    {
        //Calculations unimportant to the question
        return new RGBHelper(r,g,b)
    }

    public HSLHelper getHSL()
    {
        if(HSLValues == null)
        {
            HSLValues = RGBToHSL(RGBValues);
        }
        return HSLValues;
    }

    public RGBHelper getRGB()
    {
        if(RGBValues == null)
        {
            RGBValues = HSLToRGB(HSLValues);
        }
        return RGBValues;
    }
}

RGBColor然后HSLColor派生自的类Color,实现一个简单的构造函数来初始化RGBValuesHSLValues成员。(是的,我知道基类 if-ily 包含派生类的静态实例)

public class ColorRGB extends Color
{
    public ColorRGB(int r, int g, int b)
    {
        RGBValues = new RGBHelper(r,g,b);
    }
}

public class ColorHSL extends Color
{
    public ColorHSL(double hue, double sat, double lum)
    {
        HSLValues = new HSLHelper(hue,sat,lum);
    }
}
4

5 回答 5

8

必须在声明类型的构造函数完成时分配最终变量。因此,您不能在子类中分配 super 的 final 字段。

但是,您可以在子类的静态工厂方法中进行转换:

class HSLColor {
    private HSLColor(int r, int g, int b) { super(r,g,b);}

    static HSLColor create(int h, int s, int l) {
        // conversion code here
        return new HSLColor(r,g,b);
    }
}
于 2009-11-14T14:35:32.217 回答
3

据我所知,完成这项工作的唯一方法是将超级构造函数的调用与计算 r、g 和 b 的函数嵌套:

super(calculateRGB(...))

因此,您可能会考虑向 Color 添加一个将 RGB 值作为数组的构造函数。

于 2009-11-14T14:37:16.990 回答
2

是的,我可以看到如何super(calculateRGB(...))- 但看起来您从这里的继承中几乎没有获得任何收益。我只是使用一个通用接口。RGB 和 HSV 不只是两种不同的、可互换的颜色模型吗?

我认为Java问题背后有一个面向对象的分析问题。为什么要使用继承?

如果您需要做的只是可互换地操作 Color,您可能会发现您根本没有从继承中受益(而且它只会为超类创建将 HSV 映射回 RGB 的开销)......如果您只想可互换的颜色模型,请考虑使用 Color 接口而不是从 RGB 继承。

如果不了解您将实际使用 Color 对象的目的,就很难提出更好的设计。现在看起来继承的成本(调用超级构造函数中的颜色模型转换)将超过重用的唯一好处normalize

于 2009-11-14T14:39:52.177 回答
1

您不能在 Java 中使用抽象构造函数,因此除非您可以将所有计算都放入对 super 的调用中,否则您无法对当前设计做您想做的事情,因为最终变量必须由时间分配声明构造函数已完成。

另一种方法是考虑一些用于创建 Color 对象(工厂模式)的方法,该对象接受参数,在构造函数外部进行计算,然后您可以调用 super() 作为第一个参数。

例如 - 如果您的 Color 课程有以下内容

public Color static createHSLColor(int h, int s, int v) {
   // All the calculation here

   return new ColorHSL(h,s,v);
}

然后,您可以使构造函数不公开(可能受保护),然后创建对象的唯一方法是通过您的工厂方法。

于 2009-11-14T14:38:22.260 回答
1

您可以做的一件事是拥有一个代表计算器的构造函数参数。例如:

public Color(int r, int g, int b, Calc calc) {
   _r = calc.normalize(r);
   _g = calc.normalize(g);
   _b = calc.normalize(b);
}

这可能会完全消除对子类的需求。您可以声明构造函数:

public Color(int r, int g, int b) { 
  this(r,g,b, defaultCalc);
}

甚至提供静态样式构造函数:

public static Color hslColor(int r, int g, int b) {
    return new Color(r,g,b, hslCalc);
}
于 2009-11-14T14:38:31.900 回答