我正在阅读的一个消息来源说,关键字private
表示一个方法或变量在类级别是私有的,而不是对象级别。关键词“private”在课堂上是私密的,这意味着什么?
含义在像这样的代码块:
public class Weight2 implements Comparable<Weight2>
{
private int myPounds, myOunces;
public Weight2()
{
myPounds = myOunces = 0;
}
public Weight2(int x, int y)
{
myPounds = x;
myOunces = y;
}
public int compareTo(Weight2 w)
{
if(myPounds<w.myPounds)
return -1;
if(myPounds>w.myPounds)
return 1;
if(myOunces<w.myOunces)
return -1;
if(myOunces>w.myOunces)
return 1;
return 0;
}
}
一个1加权对象可以访问不同的重量2对象的私有字段,而存取方法...而是通过只是说w.myPounds
。
澄清:
我想从那里对象可以访问不同对象的私有数据就知道了。它仅仅来自课堂内部吗?或者这可以从驱动程序完成?
[在Java中,默认,公共,保护和私有之间的区别]的可能重复(https://stackoverflow.com/questions/215497/in-java-difference-between-default-public-protected-和私人) – DevelopingDeveloper
我的问题不是关于标签之间的差异。这是关于班级内部“私人”的含义......更多技术细节未在您引用的线索中回答。 –
“说到在课堂上给予隐私而不是对象级别,究竟意味着什么?”这意味着“...相同类型的对象[可以]不使用访问器方法访问彼此的私有字段。” –