ÎÒÃÇÖªµÀ£¬·â×°½«Êý¾ÝºÍ´¦ÀíÊý¾ÝµÄ´úÂëÁ¬½ÓÆðÀ´¡£Í¬Ê±£¬·â×°Ò²ÌṩÁíÒ»¸öÖØÒªÊôÐÔ£º·ÃÎÊ¿ØÖÆ£¨access control £©¡£Í¨¹ý·â×°Äã¿ÉÒÔ¿ØÖƳÌÐòµÄÄÄÒ»²¿·Ö¿ÉÒÔ·ÃÎÊÀàµÄ³ÉÔ±¡£Í¨¹ý¿ØÖÆ·ÃÎÊ£¬¿ÉÒÔ×èÖ¹¶ÔÏóµÄÀÄÓá£ÀýÈ磬ͨ¹ýÖ»ÔÊÐíÊʵ±¶¨ÒåµÄÒ»Ì×·½·¨À´·ÃÎÊÊý¾Ý£¬ÄãÄÜ×èÖ¹¸ÃÊý¾ÝµÄÎóÓá£Òò´Ë£¬Èç¹ûʹÓõõ±£¬¿ÉÒÔ°ÑÀà´´½¨Ò»¸ö¡°ºÚºÐ×Ó¡±£¬ËäÈ»¿ÉÒÔʹÓøÃÀ࣬µ«ÊÇËüµÄÄÚ²¿»úÖÆÊDz»¹«¿ªµÄ£¬²»ÄÜÐ޸ġ£µ«ÊÇ£¬±¾ÊéÇ°Ãæ´´½¨µÄÀà¿ÉÄܲ»»áÍêÈ«ÊʺÏÕâ¸öÄ¿±ê¡£ÀýÈ磬¿¼ÂÇÔÚµÚ6ÕÂĩβʾÀýµÄStackÀà¡£·½·¨push( ) ºÍpop() ȷʵΪ¶ÑÕ»Ìṩһ¸ö¿É¿ØÖƵĽӿڣ¬ÕâÊÇÊÂʵ£¬µ«Õâ¸ö½Ó¿Ú²¢Ã»±»Ç¿ÖÆÖ´ÐС£Ò²¾ÍÊÇ˵£¬³ÌÐòµÄÆäËû²¿·Ö¿ÉÒÔÈƹýÕâЩ·½·¨¶øÖ±½Ó´æÈ¡¶ÑÕ»£¬ÕâÊÇ¿ÉÄܵġ£µ±È»£¬Èç¹ûʹÓò»µ±£¬Õâ¿ÉÄܵ¼ÖÂÂé·³¡£±¾½Ú½«½éÉÜÄܾ«È·¿ØÖÆÒ»¸öÀà¸÷ÖÖ¸÷Ñù³ÉÔ±µÄ·ÃÎʵĻúÖÆ¡£
Ò»¸ö³ÉÔ±ÈçºÎ±»·ÃÎÊÈ¡¾öÓÚÐÞ¸ÄËüµÄÉùÃ÷µÄ·ÃÎÊָʾ·û£¨access specifier £©¡£Java ÌṩһÌ׷ḻµÄ·ÃÎÊָʾ·û¡£´æÈ¡¿ØÖƵÄijЩ·½ÃæÖ÷ÒªºÍ¼Ì³Ð»ò°üÁªÏµÔÚÒ»Æ𣨰ü£¬package£¬±¾ÖÊÉÏÊÇÒ»×éÀࣩ¡£Java µÄÕâЩ·ÃÎÊ¿ØÖÆ»úÖƽ«ÔÚÒÔºóÌÖÂÛ¡£ÏÖÔÚ£¬ÈÃÎÒÃÇ´Ó·ÃÎÊ¿ØÖÆÒ»¸ö¼òµ¥µÄÀ࿪ʼ¡£Ò»µ©ÄãÀí½âÁË·ÃÎÊ¿ØÖƵĻù±¾ÔÀí£¬ÆäËû²¿·Ö¾Í±È½ÏÈÝÒ×ÁË¡£
Java µÄ·ÃÎÊָʾ·ûÓÐpublic £¨¹«¹²µÄ£¬È«¾ÖµÄ£©¡¢private £¨Ë½Óеģ¬¾Ö²¿µÄ£©¡¢ºÍprotected £¨Êܱ£»¤µÄ£©¡£Java Ò²¶¨ÒåÁËÒ»¸öĬÈÏ·ÃÎʼ¶±ð¡£Ö¸Ê¾·ûprotected½öÓÃÓڼ̳ÐÇé¿öÖС£ÏÂÃæÎÒÃÇÃèÊöÆäËûÁ½¸ö·ÃÎÊָʾ·û¡£
ÈÃÎÒÃÇ´Ó¶¨Òåpublic ºÍprivate ¿ªÊ¼¡£µ±Ò»¸öÀà³ÉÔ±±»public ָʾ·ûÐÞÊÎʱ£¬¸Ã³ÉÔ±¿ÉÒÔ±»ÄãµÄ³ÌÐòÖеÄÈκÎÆäËû´úÂë·ÃÎÊ¡£µ±Ò»¸öÀà³ÉÔ±±»Ö¸¶¨Îªprivate ʱ£¬¸Ã³ÉÔ±Ö»Äܱ»ËüµÄÀàÖеÄÆäËû³ÉÔ±·ÃÎÊ¡£ÏÖÔÚÄãÄÜÀí½âΪʲômain( ) ×ÜÊDZ»public ָʾ·ûÐÞÊΡ£Ëü±»ÔÚ³ÌÐòÍâÃæµÄ´úÂëµ÷Óã¬Ò²¾ÍÊÇÓÉJava ÔËÐÐϵͳµ÷Óá£Èç¹û²»Ê¹Ó÷ÃÎÊָʾ·û£¬¸ÃÀà³ÉÔ±µÄĬÈÏ·ÃÎÊÉèÖÃΪÔÚËü×Ô¼ºµÄ°üÄÚΪpublic £¬µ«ÊÇÔÚËüµÄ°üÒÔÍâ²»Äܱ»´æÈ¡£¨°ü½«ÔÚÒÔºóµÄÕ½ÚÖÐÌÖÂÛ£©¡£
µ½Ä¿Ç°ÎªÖ¹£¬ÎÒÃÇ¿ª·¢µÄÀàµÄËùÓгÉÔ±¶¼Ê¹ÓÃÁËĬÈÏ·ÃÎÊģʽ£¬ËüʵÖÊÉÏÊÇpublic ¡£È»¶ø£¬Õâ²¢²»ÊÇÄãÏëÒªµÄµäÐ͵ķ½Ê½¡£Í¨³££¬ÄãÏëÒª¶ÔÀàÊý¾Ý³ÉÔ±µÄ·ÃÎʼÓÒÔÏÞÖÆ£¬Ö»ÔÊÐíͨ¹ý·½·¨À´·ÃÎÊËü¡£ÁíÍ⣬ÓÐʱÄãÏë°ÑÒ»¸ö·½·¨¶¨ÒåΪÀàµÄÒ»¸ö˽Óеķ½·¨¡£
·ÃÎÊָʾ·ûλÓÚ³ÉÔ±ÀàÐ͵ÄÆäËû˵Ã÷µÄÇ°Ãæ¡£Ò²¾ÍÊÇ˵£¬³ÉÔ±ÉùÃ÷Óï¾ä±ØÐëÒÔ·ÃÎÊָʾ·û¿ªÍ·¡£ÏÂÃæÊÇÒ»¸öÀý×Ó£º
public int i;
private double j;
private int myMethod(int a£¬char b) { // ...
ÒªÀí½âpublic ºÍprivate ¶Ô·ÃÎʵÄ×÷Ó㬿´ÏÂÃæµÄ³ÌÐò£º
/* This program demonstrates the difference between
public and private.
*/
class Test {
int a; // default access
public int b; // public access
private int c; // private access
// methods to access c
void setc(int i) { // set c's value
c = i; }
int getc() { // get c's value
return c;
}
}
class AccessTest {
public static void main(String args[]) {
Test ob = new Test();
// These are OK£¬a and b may be accessed directlyob.a = 10;ob.b = 20;
// This is not OK and will cause an error
// ob.c = 100; // Error!
// You must access c through its methodsob.setc(100); // OKSystem.out.println("a£¬b£¬and c: " + ob.a + " " +
ob.b + " " + ob.getc());
}
}
¿ÉÒÔ¿´³ö£¬ÔÚTest ÀàÖУ¬aʹÓÃĬÈÏ·ÃÎÊָʾ·û£¬ÔÚ±¾ÀýÖÐÓëpublic Ïàͬ¡£b±»ÏÔʽµØÖ¸¶¨Îªpublic ¡£³ÉÔ±c±»Ö¸¶¨Îªprivate £¬Òò´ËËü²»Äܱ»ËüµÄÀàÖ®ÍâµÄ´úÂë·ÃÎÊ¡£ËùÒÔ£¬ÔÚAccessTest ÀàÖв»ÄÜÖ±½ÓʹÓÃc¡£¶ÔËüµÄ·ÃÎÊÖ»ÄÜͨ¹ýËüµÄpublic ·½·¨£ºsetc()ºÍgetc() ¡£Èç¹ûÄ㽫ÏÂÃæÓï¾ä¿ªÍ·µÄ×¢ÊÍ·ûºÅÈ¥µô£¬
// ob.c = 100; // Error!
ÔòÓÉÓÚÎ¥¹æ£¬Äã²»ÄܱàÒëÕâ¸ö³ÌÐò¡£ÎªÁËÀí½â·ÃÎÊ¿ØÖÆÔÚʵ¼ÊÖеÄÓ¦Óã¬ÎÒÃÇÀ´¿´ÔÚµÚ6ÕÂĩβËùʾµÄStack ÀàµÄ¸Ä½ø°æ±¾¡£
// This class defines an integer stack that can hold 10 values.
class Stack {
/* Now£¬both stck and tos are private. This means that they cannot be accidentally or maliciouslyaltered in a way that would be harmful to the stack.
*/private int stck[] = new int[10]; private int tos;
// Initialize top-of-stack
Stack() {
tos = -1;
}
// Push an item onto the stack void push(int item) {if(tos==9) System.out.println("Stack is full."); else
stck[++tos] = item; }// Pop an item from the stack
int pop() {
if(tos < 0) { System.out.println("Stack underflow."); return 0;
}
else
return stck[tos--];
}
}
ÔÚ±¾ÀýÖУ¬ÏÖÔÚ´æ´¢¶ÑÕ»µÄstckºÍÖ¸Ïò¶ÑÕ»¶¥²¿µÄϱêtos £¬¶¼±»Ö¸¶¨Îªprivate ¡£ÕâÒâζ×ųýÁËͨ¹ýpush() »òpop()£¬ËüÃDz»Äܹ»±»·ÃÎÊ»ò¸Ä±ä¡£ÀýÈ磬½«tos Ö¸¶¨Îªprivate £¬×èÖ¹Äã³ÌÐòµÄÆäËû²¿·ÖÎÞÒâÖн«ËüµÄÖµÉèÖÃΪ³¬¹ýstck Êý×éϱê½çµÄÖµ¡£
ÏÂÃæµÄ³ÌÐò±íÃ÷Á˸ĽøµÄStack Àà¡£ÊÔ×ÅɾȥעÊÍÇ°ÃæµÄÏßÌõÀ´Ö¤Ã÷stckºÍtos ³ÉԱȷʵÊDz»ÄÜ·ÃÎʵġ£
class TestStack {
public static void main(String args[]) {
Stack mystack1 = new Stack();
Stack mystack2 = new Stack();
// push some numbers onto the stack
for(int i=0; i<10; i++) mystack1.push(i);
for(int i=10; i<20; i++) mystack2.push(i);
// pop those numbers off the stack
System.out.println("Stack in mystack1:");
for(int i=0; i<10; i++)
System.out.println(mystack1.pop());
System.out.println("Stack in mystack2:");
for(int i=0; i<10; i++)
System.out.println(mystack2.pop());
// these statements are not legal
// mystack1.tos = -2;
// mystack2.stck[3] = 100;
}
}
¾¡¹ÜÓÉÀඨÒåµÄ·½·¨Í¨³£Ìṩ¶ÔÊý¾ÝµÄ·ÃÎÊ£¬µ«Çé¿ö²¢²»×ÜÊÇÕâÑù¡£µ±ÐèҪʱÔÊÐíÒ»¸öʵÀý±äÁ¿Îªpublic ÊÇÍêÈ«ºÏÊʵġ£ÀýÈç,Ϊ¼òµ¥Æð¼û£¬±¾ÊéÖдó¶àÊýµÄ¼òµ¥ÀàÔÚ´´½¨Ê±²»¹ØÐÄʵÀý±äÁ¿µÄ´æÈ¡¡£È»¶ø£¬ÔÚ´ó¶àÊýʵ¼ÊÓ¦ÓõÄÀàÖУ¬Ä㽫ÓбØÒª½ö½öÔÊÐíͨ¹ý·½·¨À´¶ÔÊý¾Ý²Ù×÷¡£ÏÂÒ»Õ½«»Øµ½·ÃÎÊ¿ØÖƵĻ°Ìâ¡£Ä㽫¿´µ½£¬Ôڼ̳ÐÖзÃÎÊ¿ØÖÆÊÇÖÁ¹ØÖØÒªµÄ¡£
xiazhouquan ÓÚ 2009-09-03 15:46:30·¢±í:
¶÷£¬²»´í£¬Ö§³ÖÒ»¸ö£¬javaÓïÑÔͦÓÐǰ;µÄ£¬ºÜ»ð£¬´úÂëÒ²ºÜÃÀ
caolongday ÓÚ 2009-09-02 22:40:29·¢±í:
û¿´Ã÷°×¡¡