private的理解:private是对类而不是对对象而言!!!
class MyPoint
{
private int x, y;
private String pointName;
public MyPoint()
{
x = 0;
y = 0;
pointName = "";
}
public MyPoint(int x, int y, String pointName)
{
this.x = x;
this.y = y;
this.pointName = pointName;
}
public void printOther(MyPoint other)
{
//This is valid, for 'this' and 'other' object
//share the same class 'MyPoint'.
other.printPoint();
}
private void printPoint()
{
System.out.println("In object " + pointName);
System.out.println("x = " + x +"\ty = " +y);
}
}
public class UsePoint
{
public static void main(String[] args)
{
MyPoint start = new MyPoint(0, 0, "START");
MyPoint end = new MyPoint(20, 20, "END");
start.printOther(end);
// The following is an invalid statement, because
// UsePoint and MyPoint are two different classes.
// end.printPoint();
}
}
//The result is:
//In the object END
//x = 20 y = 20