有这么一个奇怪的例子,不知道是我的程序有问题还是我的概念错了,
在这个例子中,width 和 height分别是private 和 protected 的属性,可是我的电脑一样能把它们的数值输出。
我原来的理解是一个对象只能用对象.public的属性或方法, 我不明白,为何私有和保护的属性也可以直接输出了?
谁能帮我解释一下不?public class Rectangle {
//受保护的member
public int x;
protected int y;
private int width;
protected int height; public Rectangle() {
} public Rectangle(int x, int y,
int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
} public void setX(int x) { this.x = x; }
public void setY(int y) { this.y = y; }
public void setWidth(int width) { this.width = width; }
public void setHeight(int height) { this.height = height; } public int getX() { return x; }
public int getY() { return y; }
public int getWidth() { return width; }
public int getHeight() { return height; } public int getArea() { return width*height; }
public static void main(String[] args){
Rectangle rect=new Rectangle(10,20,30,40);
System.out.print(rect.width);
System.out.print(rect.height);
}
}
在这个例子中,width 和 height分别是private 和 protected 的属性,可是我的电脑一样能把它们的数值输出。
我原来的理解是一个对象只能用对象.public的属性或方法, 我不明白,为何私有和保护的属性也可以直接输出了?
谁能帮我解释一下不?public class Rectangle {
//受保护的member
public int x;
protected int y;
private int width;
protected int height; public Rectangle() {
} public Rectangle(int x, int y,
int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
} public void setX(int x) { this.x = x; }
public void setY(int y) { this.y = y; }
public void setWidth(int width) { this.width = width; }
public void setHeight(int height) { this.height = height; } public int getX() { return x; }
public int getY() { return y; }
public int getWidth() { return width; }
public int getHeight() { return height; } public int getArea() { return width*height; }
public static void main(String[] args){
Rectangle rect=new Rectangle(10,20,30,40);
System.out.print(rect.width);
System.out.print(rect.height);
}
}
private 允许 不允许 不允许 不允许
protected 允许 允许 允许 不允许package a;import b.Rectangle;public class Test { /**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
Rectangle r= new Rectangle(10,20,30,40);
System.out.println(r.x);
System.out.println(r.width);//private修饰 这里报错了
System.out.println(r.height);//protected 修饰 这里报错了
}}package b;public class Rectangle {
//受保护的member
public int x;
protected int y;
private int width;
protected int height; public Rectangle() {
} public Rectangle(int x, int y,
int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
} public void setX(int x) { this.x = x; }
public void setY(int y) { this.y = y; }
public void setWidth(int width) { this.width = width; }
public void setHeight(int height) { this.height = height; } public int getX() { return x; }
public int getY() { return y; }
public int getWidth() { return width; }
public int getHeight() { return height; } public int getArea() { return width*height; }
// public static void main(String[] args){
// Rectangle rect=new Rectangle(10,20,30,40);
// System.out.print(rect.width);
// System.out.print(rect.height);
// } }
你都把所有的东西全部放在一个类中了,所以private类型的变量是可以访问的.
protected 允许 允许 允许 不允许
private 允许 不允许 不允许 不允许
protected 允许 允许 允许 不允许
你这个是在一个类呀