我正在閱讀的一個消息來源說,關鍵字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
我的問題不是關於標籤之間的差異。這是關於班級內部「私人」的含義......更多技術細節未在您引用的線索中回答。 –
「說到在課堂上給予隱私而不是對象級別,究竟意味着什麼?」這意味着「...相同類型的對象[可以]不使用訪問器方法訪問彼此的私有字段。」 –