private的作用范围是:在定义的类内有效。你的情况是他们在访问自己的对象里面的private。因为是同一个类的对象所以他们有相同的成员变量,所以你在调用时,他们访问各自的。不是对方的。
解决方案 »
- 菜鸟求助:Java 程序解释
- Map接口问题---Map m=new HashMap()这句话怎么理解啊???
- 高手帮我看一下,问题处在哪里?
- 在html页面中,如何用程序判断本机是否连接到因特网?
- 高分求救,高手进来挑战下!~
- 一个菜鸟问题
- 在线等JOptionPane.showMessageDialog(null, stroutput)对齐?
- 关于javabean的使用问题!
- 怎么样能把正常情况下显示的字符串(从左到右显示)旋转90度(从上到下一个个字符显示)
- 请问如何监测某一主机是否在某一端口进行监听?
- 关于在一个实体bean中引用另一个实体bean的一点困惑?解决后立即给分(150分)
- 急!急!急!请求帮助!谢谢!!!
private int i;
You(int i){
this.i = i;
}
boolean compareTo(You y){
return this.i > y.i;
}
}
public class Ptest{
public static void main(String[] args){
You y1 = new You(4);
You y2 = new You(3);
System.out.println(y1.compareTo(y2));
}
}//在这里调用y1的compareTo方法不是就直接访问了y2.i这个私有变量了?
私有成员不是属于对象的吗?别的对象还可以存取,那还叫私有?
私有成员的值应该只能通过方法返回得到,不能直接取出。
这样不是破坏了类的封装性。
java能够这样吗?
c++是不能的。
a2里的某个元素 那么必然是A里定义了 访问该元素的方法
而private是对于class来说的 不是object
Y1并没有直接调用Y2的i,在CompareTo方法里参数是类You的一个实例(即Y2)
Y1是通过传了Y2这个参数,然后是Y2访问了私有变量i
private int i;
You(int i){
this.i = i;
}
boolean compareTo(Me m){
return this.i > m.i;
}
}class Me{
private int i;
You(int i){
this.i = i;
}
boolean compareTo(You y){
return this.i > y.i;
}
} public class Ptest{
public static void main(String[] args){
You y1 = new You(4);
Me m = new Me(3);
System.out.println(y1.compareTo(m));
}
}
the statment "a and b are the same object" is determined at runtime.
so, it is not feasible to say "method can only access private members of the object itself".
in other words, access protection is at class-level. not object level.