private的理解:private是对类而不是对对象而言!!!

leizhengdeng 2001-09-20 11:02:59

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
...全文
216 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
leizhengdeng 2001-09-29
  • 打赏
  • 举报
回复
这下大家满足了吧
leizhengdeng 2001-09-29
  • 打赏
  • 举报
回复
一个非常有用的例子:(计算任何两天的相差天数)
注意:方法DaysSpan用到了这个特性。
//////////////////////////////////////
//以前我写的C++类使用操作符重载的:
int CDate::operator-( CDate& date)
{
return this->DaysFromJesus() - date.DaysFromJesus();
}
//操作符重载在c++中的使用还是比较方便的
CDate birthday = new CDate(2000, 1, 1);
CDate today = new CDate(2001, 1, 1);
int span =today - birthday;
/////////////////////////////////////
现在用java重写了,由于java不支持操作符重载,所以改写成函数,刚好用到了这个特性:




//*************************************************
//Author: leizhengdeng(Arden)
//http://arden.3322.net leizhengdeng@163.net
//Module: class CDate
//function: to calculate the span of two days
//**************************************************

class CDate
{
static final int MONTH[ ]={0,31,28,31,30,31,30,31,31,30,31,30,31};
private int m_year;
private int m_month;
private int m_day;

public CDate()
{
m_year = 0;
m_month = 0;
m_day = 0;
}

public CDate(int year, int month, int day)
{
m_year = year;
m_month = month;
m_day = day;
}

private int DaysFromJesus()
{
int totalDays;
totalDays = 365*m_year + m_year/4 - m_year/100 + m_year/400 + 1;
if (m_year%400 == 0) totalDays--;
for(int i = 0; i < m_month; i++)
totalDays += MONTH[i];
totalDays += m_day;
if (IsLeapYear() && m_month >= 3) totalDays++;
return totalDays;
}


private boolean IsLeapYear()
{
if ((m_year%4 == 0 && m_year%100 != 0) || (m_year%400 == 0))
return true;
else
return false;
}

public int DaysSpan(CDate date)
{
return this.DaysFromJesus() - date.DaysFromJesus();
}
}

public class TestDate
{
public static void main(String[] args)
{
CDate birthday = new CDate(2000, 1, 1);
CDate today = new CDate(2001, 1, 1);
int span =today.DaysSpan(birthday);

System.out.println("span is " + span);
}
}
leizhengdeng 2001-09-29
  • 打赏
  • 举报
回复
to:tanghuan() 
我试过了,编译运行都可以啊

public void printOther(MyPoint other)
{
//This is valid, for 'this' and 'other' object
//share the same class 'MyPoint'.
System.out.println("In object " + other.pointName);
System.out.println("x = " + other.x +"\ty = " +other.y);
}
tanghuan 2001-09-29
  • 打赏
  • 举报
回复
方法private是对类而言,属性就不同了,他是对对象而言

如果你把public void printOther(MyPoint other)
改为如下试一试看看
public void printOther(MyPoint other)

{
//This is valid, for 'this' and 'other' object
//share the same class 'MyPoint'.
System.out.println("In object " + other.pointName);
System.out.println("x = " + other.x +"\ty = " +other.y);

}

freezingfire 2001-09-20
  • 打赏
  • 举报
回复
这样真的可以吗?
Necromancer 2001-09-20
  • 打赏
  • 举报
回复
哎呀说错了。。主体程序是只有一个实例,但helper类可能会有多个实例
Necromancer 2001-09-20
  • 打赏
  • 举报
回复
实例之间相互访问?私有成员?我的程序只有一个实例在跑的,不知道多实例的应用领域是什么。。赫赫。。。
虎叔 2001-09-20
  • 打赏
  • 举报
回复
确实如此!不知这个特性有什么作用,在编程上?
leizhengdeng 2001-09-20
  • 打赏
  • 举报
回复
同一类的对象可以访问同一类的另一个对象的私有成元
skyyoung 2001-09-20
  • 打赏
  • 举报
回复
??
hyhong_h 2001-09-20
  • 打赏
  • 举报
回复
什么跟什么啊!
xuxk 2001-09-20
  • 打赏
  • 举报
回复
gz
hailong326 2001-09-20
  • 打赏
  • 举报
回复
gz
leizhengdeng 2001-09-20
  • 打赏
  • 举报
回复
这样的意思就是在同一类中的对象可以互相通讯,就是封装嘛

23,407

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧