【问题标题】:variable has private access变量具有私有访问权限
【发布时间】:2023-03-23 16:16:01
【问题描述】:

我正在尝试为矩形和椭圆创建一个抽象形状类,我给形状的唯一抽象方法是 draw 方法,但是在我给它一个构造函数之后,它在矩形类中给了我一个错误,说颜色其他变量具有私有访问权限,这是我的代码:

public abstract class Shape{
        private int x, y, width, height;
        private Color color;
        public Shape(int x, int y, int width, int height, Color color){
            setXY(x, y);
            setSize(width, height);
            setColor(color);
        }

        public boolean setXY(int x, int y){
            this.x=x;
            this.y=y;
            return true;
        }

        public boolean setSize(int width, int height){
            this.width=width;
            this.height=height;
            return true;
        }

        public boolean setColor(Color color){
            if(color==null)
                return false;
            this.color=color;
            return true;
        }

        public abstract void draw(Graphics g);
    }

    class Rectangle extends Shape{
        public Rectangle(int x, int y, int width, int height, Color color){
            super(x, y, width, height, color);
        }

        public void draw(Graphics g){
            setColor(color);
            fillRect(x, y, width, height);
            setColor(Color.BLACK);
            drawRect(x, y, width, height);
        }
    }

    class Ellipse extends Shape{
        public Ellipse(int x, int y, int width, int height, Color color){
            super(x, y, width, height, color);
        }

        public void draw(Graphics g){
            g.setColor(color);
            g.fillOval(x, y, width, height);
            g.setColor(Color.BLACK);
            g.drawOval(x, y, width, height);
        }
    }

【问题讨论】:

    标签: java variables subclass private abstract


    【解决方案1】:
    g.setColor(color);
    g.fillOval(x, y, width, height);
    

    所有这些字段都是父类私有的。

    您不能从类外部访问私有字段,甚至不能从子类访问。

    您可以将字段更改为受保护的或为子类提供要调用的 getter。

    所以在Shape中,添加一个方法

    protected Color getColor(){  return color; }
    

    然后你就可以了

    g.setColor(getColor());
    

    在您的子类中。其他字段也一样。

    【讨论】:

    • 即使在子类中也不能访问私有变量。您可以将该变量更改为受保护的(以便它可以在子类和同一包中的类中访问),也可以在子类中使用 get color。
    【解决方案2】:

    private int x, y, width, height; 表示它们只能从声明它们的实际类中访问。您应该创建适当的 getset 方法并使用它们。您希望字段为 publicprotected 以使用点符号访问它们,但 IMO 将它们保密并使用 getset 是更好的设计。另请参阅解释字段可见性的 In Java, difference between default, public, protected, and private

    【讨论】:

    • 很抱歉,我无法理解如何使用 set 和 get 方法,您能提供一个直观的示例吗?
    • 在超类中为所有私有字段添加访问器方法,例如public int getX(){return this.x;} 等...然后在子类(例如Ellipse)中使用它们,例如:g.fillOval(getX(), getY(), getWidth(), getHeight());
    • 因此,如果我理解正确,您是说我需要将 get 方法添加到我的超类(因为我很确定我有 setter)并将它们设置为返回 true,然后将它们放入在子类中替换括号中的所有变量?
    • 您需要在超类中添加get方法,并将它们设置为返回必须返回的内容(例如public int getWidth(){return this.width;}public int getX(){return this.x;})并使用它们来检索字段值而不是直接访问字段
    • 好吧,我照你说的做了,给所有的setter添加了get方法,让它们返回true,我把get方法放在子类中,就像你的例子一样,但现在说getColor方法不能应用给定类型。那么发生了什么?
    猜你喜欢
    • 2020-03-01
    • 2013-10-27
    • 2014-08-09
    • 2015-12-25
    • 2014-11-24
    • 1970-01-01
    • 2012-05-22
    • 1970-01-01
    相关资源
    最近更新 更多