Java 变量具有私有访问权限
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23463427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
variable has private access
提问by user3602628
I'm trying to make an abstract shape class for rectangle and ellipse the only abstract method I gave shape was the draw method, but after I gave it a constructor and everything it gives me an error in the rectangle class saying that color and the other variables have private accesses here is my code:
我正在尝试为矩形和椭圆创建一个抽象形状类,我给形状的唯一抽象方法是 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);
}
}
回答by Thilo
g.setColor(color);
g.fillOval(x, y, width, height);
All these fields are private to the parent class.
所有这些字段都是父类私有的。
You cannot access a private field from outside the class, not even from subclasses.
您不能从类外部访问私有字段,甚至不能从子类访问。
You could change the field to protected or provide getters for the subclasses to call.
您可以将该字段更改为 protected 或为要调用的子类提供 getter。
So in Shape, add a method
所以在Shape中,添加一个方法
protected Color getColor(){ return color; }
and then you can do
然后你可以做
g.setColor(getColor());
in your subclasses. Same for the other fields.
在你的子类中。其他领域也一样。
回答by Totò
private int x, y, width, height;
means that they can only be accessed from the actual class where they are declared. You should create appropriate get
and set
methods and use them. You want the fields to be either public
or protected
to access them using the dot notation, but IMO it's a better design to keep them private and use get
and set
. See also In Java, difference between default, public, protected, and privatethat explains the fields' visibility.
private int x, y, width, height;
意味着它们只能从声明它们的实际类中访问。你应该建立适当get
和set
方法,并使用它们。你想要的字段是要么public
或protected
访问他们使用点符号,但国际海事组织这是一个更好的设计,以保持他们的私人和使用get
和set
。另请参阅在 Java 中,解释字段可见性的默认、公共、受保护和私有之间的区别。