Java °æ (¾«»ªÇø)
·¢ÐÅÈË: wazy (·Ø), ÐÅÇø: Java
±ê Ìâ: javaÓïÑԹ淶£¨2£©
·¢ÐÅÕ¾: ×϶¡Ïã (Tue Sep 16 16:08:55 1997)
4. Àà
Àà(class)ÊÇ´«Í³ÃæÏò¶ÔÏó±à³ÌÄ£Ð͵ÄÏóÕ÷¡£ËüÃÇÖ§³ÖÊý¾Ý³éÏóÒÔ¼°Êµ
ÏÖ¶ÔÊý¾ÝµÄÔ¼Êø£¬ÔÚJavaÖУ¬Ã¿Ò»¸öеÄÀà´´½¨Ò»¸öÐÂÀàÐÍ¡£
ÒªÏëµÃµ½Ò»¸öеÄÀ࣬³ÌÐòÔ±±ØÐëÊ×ÏÈÕÒµ½Ò»¸öÒÑÓеÄÀ࣬ÐÂÀ༴
ÔÚÕâ¸öÒÑÓÐÀàµÄ»ù´¡ÉϹ¹Ô죬ÎÒÃdzÆ֮ΪÅÉÉú(derived)¡£ÅÉÉú³öµÄÀàÒà³Æ
ΪÔÀ´ÀàµÄ×ÓÀ࣬¶øÕâ¸öÀàÎÒÃdzÆΪ³¬Àà(super class)¡£
ÀàµÄÅÉÉú¾ßÓд«µÝÐÔ£º Èç¹ûBÊÇAµÄ×ÓÀ࣬CÊÇBµÄ×ÓÀ࣬ÔòC
ÊÇAµÄ×ÓÀà¡£
Ò»¸öÀàµÄÖ±½Ó³¬ÀàÒÔ¼°Ö¸Ê¾Õâ¸öÀàÈçºÎʵÏֵĽçÃæ(interface)£¬ÔÚÀà
µÄÉùÃ÷ÖУ¬Óɹؼü×Öextends¼°implements±ê³ö¡£ÈçÏÂʾ(ºÚÌå±íʾ¹Ø¼ü×Ö)£º£º
[doc_ comment] [modifer] class ClassName
extends Superclassname
implements interface {£¬interface } ] {
class body
}
¾ÙÀý£º
/** 2 dimension point */
public class Points {
float x£¬y;
......
}
/** printable point */
class PinttablePoint extends Points implements Printable {
......
public void Print ( ) {
}
}
ËùÓеÄÀà¾ù´ÓÒ»¸ö¸ùÀà ObjectÖÐÅÉÉú³öÀ´¡£³ýObjectÖ®ÍâµÄÈκÎÀà
¶¼ÓÐÒ»¸öÖ±½Ó³¬Àà¡£Èç¹ûÒ»¸öÀàÔÚÉùÃ÷ʱδָÃ÷ÆäÖ±½Ó³¬À࣬ÄÇôȱʡ
¼´ÎªObject¡£ÈçÏÂÊö£º
class Point {
float x£¬y
}
ÓëÏÂÃæд·¨µÈ¼Û
class Point extends Object {
float x£¬ y£»
}
JavaÓïÑÔ½öÖ§³Öµ¥¼Ì³Ð£¬Í¨¹ýÒ»¸ö±»³Æ×÷¡°½çÃ桱µÄ»úÖÆ£¬À´Ö§³Ö
ijЩÔÚÆäËüÓïÑÔÖÐÓöà¼Ì³ÐʵÏֵĻúÖÆ(Ïê¼û¡°½çÃ桱һ½Ú)¡£JavaÖ®Ëù
ÒÔûÓвÉÓÃC++µÄ¶à¼Ì³Ð»úÖÆ£¬ÊÇΪÁ˱ÜÃâ¶à¼Ì³Ð´øÀ´µÄÖî¶à²»±ã£¬Àý
È磺¿ÉÄܲúÉúµÄ¶þÒåÐÔ£¬±àÒëÆ÷¸ü¼Ó¸´ÔÓ£¬³ÌÐòÄÑÒÔÓÅ»¯µÈÎÊÌâ¡£
4.1 ÀàÀàÐÍÖ®¼äµÄÇ¿ÖÆת»»
JavaÓïÑÔÖ§³ÖÔÚÁ½¸öÀàÐÍÖ®¼äµÄÇ¿ÖÆת»»£¬ÒòΪÿ¸öÀ༴ÊÇÒ»¸öÐÂ
µÄÀàÐÍ¡£JavaÖ§³ÖÀàÀàÐÍÖ®¼äµÄÇ¿ÖÆת»»£¬Èç¹ûBÊÇAµÄ×ÓÀ࣬ÄÇôB
µÄÒ»¸öʵÀýÒà¿É×÷ΪAµÄʵÀýÀ´Ê¹Óã¬ËäÈ»²»ÐèÒªÏÔʽµÄת»»£¬µ«ÏÔʽ
ת»»ÒàÊǺϷ¨µÄ£¬Õâ±»³Æ×÷ÍØ¿í(widening)¡£Èç¹ûAµÄÒ»¸öʵÀý£¬Ïëµ±×÷
BµÄʵÀýʹÓ㬳ÌÐòÔ±¾ÍӦдһ¸öÀàÐÍת»»½Ð×÷Ï÷Õ£¨narrowing£©µÄÇ¿
ÖÆ¡£´ÓÒ»¸öÀൽÆä×ÓÀàµÄÇ¿ÖÆת
»»ÔÚÔËÐÐʱҪ×÷ÀýÐеļì²éÒÔÈ·±£Õâ¸ö¶ÔÏó¾ÍÊÇÆä×ÓÀàµÄÒ»¸öʵÀý
(»òÆä×ÓÀàÖ®Ò»)¡£ÐÖµÜÀàÖ®¼äµÄÇ¿ÖÆÀàÐÍת»»ÊÇÒ»¸ö±àÒë´íÎó£¬ÀàµÄÇ¿ÖÆת»»µÄÓï·¨ÈçÏ£
º
(classname) ref
ÆäÖУ¬(classname)ÊÇҪת»»µÄÄ¿µÄÀ࣬¶ørefÊDZ»×ª»»µÄ¶ÔÏó¡£
Ç¿ÖÆת»»½ö½öÓ°Ïìµ½¶ÔÏóµÄÒýÓ㬶ø²»»áÓ°Ïì¶ÔÏó±¾Éí¡£È»¶ø£¬¶ÔʵÀý
±äÁ¿µÄ·ÃÎÊÈ´Êܵ½¶ÔÏó
ÒýÓõÄÀàÐ͵ÄÓ°Ïì¡£Ò»¸ö¶ÔÏó´ÓÒ»¸öÀàÐ͵½ÁíÒ»ÀàÐ͵ÄÇ¿ÖÆת»»ºó£¬
¿ÉÒÔʹͬһ±äÁ¿Ãû¶Ô²»Í¬µÄʵÀý±äÁ¿·ÃÎÊ¡£
class ClassA{
String name = "ClassA"
}
class ClassB extends ClassA { //ClassBÊÇClassAµÄ×ÓÀà
String name="ClassB";
}
class AccessTest {
void test( ) {
ClassB b=new ClassB( );
println (b.name); //´òÓ¡£º ClassB
ClassA a
a=(ClassA)b;
println (a.name); //´òÓ¡£º ClassA
}
}
4.2 ·½·¨
·½·¨(method)ÊÇ¿ÉÊ©ÓÚ¶ÔÏó»òÀàÉϵIJÙ×÷£¬ËüÃǼȿÉÔÚÀàÖУ¬Ò²¿ÉÔÚ
½çÃæÖÐÉùÃ÷¡£µ«ÊÇËûÃÇÈ´Ö»ÄÜÔÚÀàÖÐʵÏÖ(JavaÖÐËùÓÐÓû§¶¨ÒåµÄ²Ù×÷¾ù
Ó÷½·¨À´ÊµÏÖ)¡£
ÀàÖеķ½·¨ÉùÃ÷°´ÒÔÏ·½Ê½£º
[Doc_ comment] [Access Specifiers] ReturnType methodName(parameterList)
{
method body(±¾µØµÄnative¼°³éÏóµÄ·½·¨Ã»ÓÐÌ岿·Ö)
}
³ý¹¹Ô캯Êý¿ÉÒÔÎÞ·µ»ØÀàÐÍÍ⣬ÆäÓàµÄ·½·¨¶¼ÓÐÒ»¸ö·µ»ØÀàÐÍ¡£Èç
¹ûÒ»¸ö²»Êǹ¹Ô캯ÊýµÄ·½·¨²»·µ»ØÈκÎÖµ£¬ÄÇôËü±ØÐëÓÐÒ»¸övoidµÄ·µ
»ØÀàÐÍ¡£
²ÎÊý±íÓɶººÅ·Ö¸ôµÄ³É¶ÔµÄÀàÐͼ°²ÎÊýÃû×é³É£¬Èç¹û·½·¨ÎÞ²ÎÊý£¬
Ôò²ÎÊý±íΪ¿Õ¡£·½·¨ÄÚ²¿¶¨ÒåµÄ±äÁ¿(¾Ö²¿±äÁ¿)²»ÄÜÒþ²Øͬһ·½·¨µÄÆä
Ëü±äÁ¿»ò²ÎÊý¡£ÀýÈ磺 Èç¹ûÒ»¸ö·½·¨´øÒÔÃûΪiµÄ²ÎÊýʵÏÖ£¬ÇÒ·½·¨ÄÚ
ÓÖ¶¨ÒåÒ»¸öÃûΪiµÄ¾Ö²¿±äÁ¿£¬Ôò»á²úÉú±àÒë´íÎó£¬ÀýÈ磺
class Rectangle {
void vertex (int i£¬int j) {
for (int i=0; i<=100; i++) { //³ö´í
¡
}
}
}
·½·¨ÌåÄÚÑ»·Óï¾äÖÐÉùÃ÷µÄiÊÇÒ»¸ö±àÒë´íÎó¡£
JavaÓïÑÔÔÊÐí¶à̬·½·¨ÃüÃû£¬¼´ÓÃÒ»¸öÃû×ÖÉùÃ÷·½·¨£¬Õâ¸öÃû×ÖÒÑ
ÔÚÕâ¸öÀà»òÆ䳬ÀàÖÐʹÓùý£¬´Ó¶øʵÏÖ·½·¨µÄ¸²¸Ç(overriding)¼°ÖØÔØ
(overloadding)¡£Ëùν¸²¸ÇÊǶԼ̳ÐÀ´µÄ·½·¨ÌṩÁíÒ»ÖÖ²»Í¬µÄʵÏÖ¡£¶ø
ÖØÔØÊÇÖ¸ÉùÃ÷Ò»¸ö·½·¨£¬ËüÓëÁíÍâÒ»¸ö·½·¨ÓÐÏàͬµÄÃû×Ö£¬µ«²ÎÊý±í²»Í¬¡£
×¢£º ·µ»ØÀàÐͲ»ÄÜÓÃÀ´Çø±ð·½·¨£¬¼´ÔÚÒ»¸öÀàµÄ·¶Î§ÄÚ£¬¾ßÓÐÏàͬ
µÄÃû×Ö£¬ÏàͬµÄ²ÎÊý±í(°üÀ¨¸öÊý¡¢Î»Öü°ÀàÐÍ)µÄ·½·¨£¬±ØÐë·µ»ØÏàͬ
µÄÀàÐÍ¡£ÈôÕâÑùµÄÁ½¸ö·½·¨Óв»Í¬µÄ·µ»ØÀàÐÍ£¬½«»á²úÉúÒ»¸ö±àÒë´íÎó¡£
4.2.1 ʵÀý±äÁ¿
ʵÀý±äÁ¿(instance variables)ÊÇÖ¸ÄÇЩÔÚÀàÄÚÉùÃ÷£¬µ«ÔÚ·½·¨µÄ×÷ÓÃ
ÓòÖ®ÍâÉÐδ±»static±ê¼ÇµÄ±ä( ²ÎÕÕ ¡°¾²Ì¬·½·¨£¬±äÁ¿¼°³õʼ»¯¡±¶Î)¡£
¶øÔÚÒ»¸ö·½·¨µÄ×÷ÓÃÓòÖ®ÄÚÉùÃ÷µÄ±äÁ¿ÊǾֲ¿±äÁ¿¡£ÊµÀý±äÁ¿¿ÉÒÔÓÐÐÞ
Êηû(¼ûÐÞÊηû)¡£
ʵÀý±äÁ¿¿ÉÒÔÊÇÈκεÄÀàÐÍ£¬²¢ÇÒ¿ÉÒÔÓгõʼֵ¡£Èç¹ûÒ»¸öʵÀý±ä
Á¿ÎÞ³õʼֵ£¬Ëü½«±»³õʼ»¯³É0¡£²¼¶ûÐͱäÁ¿±»³õʼ»¯³Éflase£¬¶ÔÏó±»
³õʼ»¯³Énull¡£ÏÂÃæÊÇÒ»¸öʵÀý±äÁ¿j¾ßÓгõʼ»¯ÖµµÄÀý×Ó£º
class A{
int j =23;
¡¡
}
4.2.2 this ºÍsuper±äÁ¿
ÔÚÒ»¸ö·Ç¾²Ì¬·½·¨µÄ×÷ÓÃÓòÄÚ£¬thisÕâ¸öÃû×Ö´ú±íÁ˵±Ç°¶ÔÏó¡£Àý
È磺 Ò»¸ö¶ÔÏó¿ÉÄÜÐèÒª°Ñ×Ô¼º×÷Ϊ²ÎÊý´«¸øÁíÒ»¸ö¶ÔÏóµÄ·½·¨£º
class MyClass {
void Method (OtherClass obj) {
¡
obj.Method (this)
¡
}
}
²»ÂÛºÎʱ£¬Ò»¸ö·½·¨ÒýÓÃËü×Ô¼ºµÄʵÀý±äÁ¿¼°·½·¨Ê±£¬ÔÚÿ¸öÒýÓÃ
µÄÇ°Ã涼Òþº¬×Å¡°this¡±¡£
È磺
class Foo {
int a£¬b£¬c;
......
void myPrint ( ) {
print (a+ "\n"); // a=="this.a"
}
......
}
super±äÁ¿ÀàËÆÓÚthis±äÁ¿¡£this±äÁ¿Êµ¼ÊÉÏÊǶԵ±Ç°¶ÔÏóµÄÒýÓã¬
ËüµÄÀàÐ;ÍÊÇ°üº¬µ±Ç°ÕýÔÚ´¦ÀíµÄ·½·¨µÄÀà¡£¶øsuper±äÁ¿ÔòÊǶÔÆ䳬Àà
ÀàÐͶÔÏóµÄÒýÓá£
4.2.3 ÉèÖþֲ¿±äÁ¿
·½·¨¶¼Òª¾¹ýÑϸñµÄ¼ì²é£¬ÒÔÈ·±£ËùÓеľֲ¿±äÁ¿(ÔÚ·½·¨ÄÚÉùÃ÷µÄ
±äÁ¿)ÔÚ±»ÒýÓÃ֮ǰÒÑÉè³õ£¬±»³õʼ»¯Ö®Ç°¾ÍʹÓþֲ¿±äÁ¿ÊÇÒ»¸ö±àÒë´í
Îó¡£
4.3 ¸²¸Ç·½·¨
ÒªÏ븲¸Ç(overiding)Ò»¸ö·½·¨£¬±ØÐëÔÚÉùÃ÷Õâ¸ö·½·¨µÄÀàµÄ×ÓÀàÖÐÉù
Ã÷Ò»¸öÓëËü¾ßÓÐÏàͬÃû×Ö£¬Ïàͬ·µ»ØÀàÐÍ£¬ÒÔ¼°Ïàͬ²ÎÊý±íµÄ·½·¨¡£µ±
×ÓÀàµÄʵÀýµ÷ÓÃÕâ¸ö·½·¨Ê±£¬±»µ÷ÓõÄÊÇж¨ÒåµÄ·½·¨¶ø²»ÊÇ×î³õµÄÄÇ
¸ö¡£±»¸²¸ÇÁ˵ķ½·¨¿Éͨ¹ýsuper±äÁ¿À´µ÷Óã¬ÈçÏ£º
setThermostat(¡) //Ö¸¸²¸ÇµÄ·½·¨
super. setThermostat(¡) //Ö¸±»¸²¸ÇµÄ·½·¨
4.4 ÖØÔصÄÈ϶¨
ÖØÔØ(overload)µÄ·½·¨ÓëÒÑÓеÄij¸ö·½·¨Í¬Ãû£¬µ«ÊDZäÔªµÄ¸öÊý¼°/
»òÀàÐͲ»Í¬£¬ÖØÔصÄÈ϶¨ÊÇÒª¾ö¶¨µ÷ÓõÄÊÇÄÄÒ»¸ö·½·¨£¬ÔÚÈ϶¨ÖØÔصÄ
·½·¨Ê±£¬²»¿¼ÂÇ·µ»ØÀàÐÍ¡£·½·¨¿ÉÒÔÔÚͬһÀàµÄÄÚ²¿ÖØÔØ£¬ÀàÄÚ²¿·½·¨
ÉùÃ÷µÄ˳Ðò²¢²»ÖØÒª¡£
¿Éͬʱ¸Ä±ä±äÔª¸öÊýºÍÀàÐÍÀ´ÊµÏÖ·½·¨µÄÖØÔØ¡£ ±àÒëÆ÷È϶¨ÏàÆ¥Åä
µÄ·½·¨Ê±ÒÔ×îµÍµÄÀàÐÍת»»¿ªÏúΪ׼¡£Ö»ÓоßÓÐÏàͬÃû×Ö¼°Ïàͬ±äÔª¸ö
ÊýµÄ·½·¨²Å»áÓÅÏÈ¿¼ÂÇ¡£ËùÓбäÔª¶¼±ØÐëת»»£¬ÊÇÆ¥Åä·½·¨Ê±µÄ×î´ó¿ª
Ïú¡£ÓÐÁ½ÖÖ±äÔªÀàÐÍÊDZØÐ迼Âǵģº¶ÔÏóÀàÐͺͻù±¾ÀàÐÍ¡£
¶ÔÏóÀàÐͼäת»»µÄ¿ªÏúµÄ´óСָÀàÊ÷ÉÏʵÔÚ²ÎÊýµÄÀàÓëÔÐͲÎÊýµÄ
ÀàÖ®¼äÁ¬ÏߵĸöÊý¡£Ö»¿¼ÂÇÍØ¿íÐÍת»»(¶ÔÏóÀàÐÍת»»µÄÏêϸÐÅÏ¢£¬Çë²Î
ÔÄ¡°ÀàÖ®¼äµÄÇ¿ÖÆÀàÐÍת»»¡±)¡£¶ÔÓÚÀàÐÍÍêÈ«Æ¥ÅäµÄ±äÔª£¬ÎÞÐë½øÐÐת
»»£¬ËüÃǵĿªÏúΪÁã¡£
»ù±¾ÀàÐ͵Äת»»¿ªÏú°´Ï±í¼ÆË㣬¿ªÏúΪÁãÊÇÑϸñÆ¥Åä¡£
to
byte short char int long float double
byte 0 1 2 3 4 6 7
short 10 0 10 1 2 4 5
from char 11 10 0 1 2 4 5
int 12 11 11 0 1 5 4
long 12 11 11 10 0 6 5
float 15 14 13 12 11 0 1
double 16 15 14 13 12 10 0
×¢£º¿ªÏú >=10 Ò×ÒýÆðÊý¾Ý¶ªÊ§¡£
Ò»¾È϶¨Ä³Ò»Æ¥Åä·½·¨ÊÇÄÄÖÖת»»¿ªÏú£¬±àÒëÆ÷ÔòÑ¡ÓÃת»»¿ªÏú×î
СµÄÆ¥Åä¡£Èç¹ûÓжàÓÚÒ»¸ö·½·¨£¬Æä×îС¿ªÏúÏàͬ£¬ÔòÆ¥ÅäÓжþÒåÐÔ£¬
Òª³ö±àÒëʱµÄ´íÎó¡£
ÀýÈ磺
class A{
int method (Object o, Thread t);
int method(Thread t,Object o);
void g(Object o,Thread t){
method(o,t); //µ÷ÓõÚÒ»¸ö·½·¨
method(t,o); //µ÷Óõڶþ¸ö·½·¨
method(t,t); //ÓжþÒåÐÔ£¬±àÒë³ö´í
£ý
£ý
4.5 ¹¹Ô캯Êý
¹¹Ô캯Êý(constructor)ÊÇÌṩ³õʼ»¯µÄרÓ÷½·¨¡£ËüºÍÀàµÄÃû×ÖÏàͬ£¬
µ«Ã»ÓÐÈκηµ»ØÀàÐÍ¡£¹¹Ô캯ÊýÔÚ¶ÔÏó´´½¨Ê±±»×Ô¶¯µØµ÷Óã¬Ëü²»Äܱ»
¶ÔÏóÏÔʽµ÷Óá£Èç¹ûÄãÏëÔÚ°ü(package)Ö®Íâµ÷Óù¹Ô캯Êý£¬¾Í½«¹¹Ô캯
ÊýÉèΪ¡°public¡±(²Î¼û¡°·ÃÎÊÖ¸Ã÷·û¡°Ò»½Ú)¡£
¹¹Ô캯ÊýÒ²¿ÉÒÔÓò»Í¬¸öÊýºÍÀàÐ͵IJÎÊýÖØÔØ£¬¾ÍÏóÆäËüµÄ·½·¨±»
ÖØÔØÒ»Ñù¡£
class Foo {
int x;
float y;
Foo() {
x=0;
y=0.0;
}
Foo (int a ) {
x=a;
y=0.0;
}
Foo (float a ) {
x=0;
y=a;
}
Foo (int a£¬float b ) {
x=a;
y=b;
}
static void myFoo( ) {
Foo obj1=new Foo( ); //µ÷ÓÃFoo( );
Foo obj2=new Foo(4 ); //µ÷ÓÃFoo( int a );
Foo obj3=new Foo(4.0 ); //µ÷ÓÃFoo( float a );
Foo obj4=new Foo(4,4.0 ); //µ÷ÓÃFoo(int a £¬ float b );
}
}
³¬ÀàµÄʵÀý±äÁ¿Óɵ÷Óó¬ÀàµÄ»òµ±Ç°ÀàµÄ¹¹Ô캯Êý³õʼ»¯¡£Èç¹ûÔÚ
´úÂëÖÐûÓÐÖ¸¶¨ÓÉ˳õʼ»¯£¬Ôòµ÷ÓõÄÊdz¬ÀàÖеÄÎÞ²ÎÊýµÄ¹¹Ô캯Êý¡£
Èç¹ûÒ»¸ö¹¹Ô캯Êýµ÷ÓÃÁËÕâ¸öÀà»òÆäÖ±½Ó³¬ÀàµÄÁíÒ»¸ö¹¹Ô캯Êý£¬Õâ¸ö
µ÷ÓñØÐëÊǹ¹Ô캯ÊýÌåµÄµÚÒ»ÌõÓï¾ä£¬ÔÚµ÷Óù¹Ô캯Êý֮ǰʵÀý±äÁ¿²»
ÒýÓá£
µ÷ÓÃÖ±½Ó³¬ÀàµÄ¹¹Ô캯ÊýÈçÏ£º
class MyClass extends OtherClass {
MyClass (someParamenters ) {
/* µ÷Óø¸À๹Ô캯Êý */
super(otherParameters);
} ¡
}¡
µ÷Óõ±Ç°ÀàµÄ¹¹Ô캯ÊýÈçÏÂʾ£º
class MyClass extends OtherClass {
MyClass (someParameters) {
¡
}
MyClass(otherParameters) {
/*µ÷Óõ±Ç°ÀàµÄ¹¹Ô캯Êý£¬¸Ãº¯ÊýÓÐÖ¸¶¨µÄ²ÎÊý±í*/
this (someParameters);
¡
}
¡
}
ÏÂÃæµÄFooºÍFooSubÀàµÄ·½·¨ÖÐʹÓù¹Ô캯ÊýµÄÀý×Ó£º
class Foo extends Bar {
int a;
Foo(int a) {
//Òþº¬µ÷ÓÃBar( )
this.a=a;
}
Foo( ) {
this (42); //µ÷ÓÃFoo(42)£¬´úÌæBar( )
}
}
class FooSub extends Foo {
int b;
FooSub (int b) {
super(13); //µ÷ÓÃFoo(13); È¥µô´ËÐн«µ÷ÓÃFoo( )
this.b=b;
}
}
4.6 ÓÃnewÔËËã·û´´½¨¶ÔÏó
ÀàÊÇÓÃÀ´¶¨Òå¶ÔÏóµÄ״̬ºÍÐÐΪµÄÄ£°å£¬¶ÔÏóÊÇÀàµÄʵÀý¡£ÀàµÄËù
ÓÐʵÀý¶¼·ÖÅäÔÚ¿É×÷ÎÞÓõ¥Ôª»ØÊյĶÑÖС£ÉùÃ÷Ò»¸ö¶ÔÏóÒýÓò¢²»»áΪ
¸Ã¶ÔÏó·ÖÅä´æ´¢£¬³ÌÐòÔ±±ØÐëÏÔʽµØΪ¶ÔÏó·ÖÅä´æ´¢£¬µ«²»±ØÏÔʽɾ³ý
´æ´¢£¬ÎÞÓõ¥Ôª»ØÊÕÆ÷»á×Ô¶¯»ØÊÕÎÞÓõÄÄÚ´æ¡£
·ÖÅä¶ÔÏó´æ´¢ÓÃnewÔËËã·û¡£³ýÁË·ÖÅä´æ´¢Ö®Í⣬new»¹³õʼ»¯Êµ
Àý±äÁ¿£¬µ÷ÓÃʵÀýµÄ¹¹Ô캯Êý¡£¹¹Ô캯ÊýÊdzõʼ»¯¶ÔÏóµÄÒ»ÖÖ·½·¨(¼û¡°¹¹
Ô캯Êý¡±)£¬ÏÂÃæµÄÀý×ÓÊÇ·ÖÅä²¢³õʼ»¯ClassAµÄÒ»¸öʵÀý£º
a = new ClassA( );
ÒÔϹ¹Ô캯ÊýÊÇ´øÓвÎÊýµÄÀý×Ó£º
b = new ClassA(3,2);
·ÖÅäµÄµÚÈýÖÖÐÎʽÔÊÐíÒÔ´®±í´ïʽÐÎʽÌṩÀàÃû×Ö£¬×Ö·û´®ÔÚÔËÐÐ
ʱ¿ÌÇóÖµ£¬new·µ»ØÒ»¸öObjectÀàÐ͵ĶÔÏó£¬ÔÙת»»³ÉËùÏ£ÍûµÄÀàÐÍ¡£
b = new ("class"+"A");
ÕâÖÖÇé¿öÏ£¬µ÷Óù¹Ô캯ÊýʱÎÞÐè²ÎÊý¡£
4.6.1 ÎÞÓõ¥ÔªÊÕ¼¯
ÎÞÓõ¥ÔªÊÕ¼¯Æ÷(Garbage Collector)ʹ´æ´¢¹ÜÀíµÄÐí¶à·½Ãæ±äµÃ¼ò
µ¥¡¢°²È«¡£³ÌÐò²»ÐèÒªÏÔʽÊÍ·ÅÄڴ棬ËüÊÇ×Ô¶¯´¦ÀíµÄ¡£ÎÞÓõ¥ÔªÊÕ¼¯
Æ÷²»»áÊÍ·ÅÕýÔÚ±»ÒýÓõÄÄڴ棬ֻÊÍ·ÅÄÇЩ²»ÔÙ±»ÒýÓõĿռ䡣Õâ¾Í·À
Ö¹ÁËÐü¹ÒÖ¸ÕëºÍ´æ´¢Â©¶´(leak)¡£ËüҲʹÉè¼ÆÈËÔ±´ÓϵͳµÄ´æ´¢¹ÜÀíÖнâ
ÍѳöÀ´¡£
4.6.2ÖÕÖ¹
JavaµÄ¹¹Ô캯ÊýÍê³ÉÒ»¸ö¶ÔÏó³õʼ»¯¹¤×÷£¬JavaµÄÖÕÖ¹
(finalization)·½·¨ÔòÍê³ÉºÍÎö¹¹º¯ÊýÀàËƵŦÄÜ£¬µ«ÓëC++²»Í¬Ö®´¦£¬ÊÇ
Ëü¿ÉÏÔʽµ÷Óá£ËäÈ»JavaµÄÎÞÓõ¥ÔªµÄ»ØÊÕ¹¦ÄÜ¿ÉÒÔ×Ô¶¯µØÊͷŶÔÏóËù
Õ¼ÓеÄ×ÊÔ´£¬µ«ÊǶÔÏóÕ¼ÓеÄijЩ×ÊÔ´£¬ÀýÈ磺 ÎļþÃèÊö·û¡¢Ì×½Ó×Ö
(socket)£¬ÎÞÓõ¥Ôª»ØÊÕÊÇÎÞ·¨´¦ÀíµÄ¡£ËùÒÔ³ÌÐòÔ±±ØÐëÓÃÖÕÖ¹º¯ÊýÀ´´¦
Àí¡£ÖîÈ磺¹Ø±Õ´ò¿ªµÄÎļþ£¬ÖÕÖ¹ÍøÂçÁ¬½ÓµÈ³ÌÐòÉƺó¹¤×÷¡£
ÏÂÃæµÄÀý×ÓÊÇÈ¡×ÔJava FileOutpntStreamÀàÖеÄÖÕÖ¹º¯Êý¡£Õâ¸öÖÕÖ¹
º¯ÊýÊÇÒ»¸öʵÀýµÄ·½·¨£¬Ã»ÓвÎÊý£¬Ò²ÎÞ·µ»ØÖµ£¬Ãû×Ö±ØÐëÈ¡finalize()¡£
ÕâÀïÎÒÃÇӦעÒâËüºÍC++Îö¹¹º¯ÊýµÄÇø±ð£º
/**
* closes the stream when garbage is collected
* checks the file descriptor first to make sure it
* is not already closed.
*/
protected void finalize( ) throws IOException {
if (fd!=null) close();
}
ÏÂÃæÊǹØÓÚÖÕÖ¹º¯ÊýµÄ¼¸µã×¢ÒâÊÂÏ
* Èç¹ûÒ»¸ö¶ÔÏóº¬ÓÐÖÕÖ¹º¯Êý£¬ÄÇôÕâ¸ö·½·¨£¬ÔÚϵͳÊÕ¼¯¸Ã¶ÔÏóËùÕ¼
ÄÚ´æ֮ǰ±»µ÷Óá£
* Java½âÊÍÆ÷¿ÉÄÜÔÚûÓÐÍê³ÉÎÞÓõ¥ÔªÊÕ¼¯µÄÇé¿öÏÂÍ˳ö£¬ÄÇôijЩÖÕ
Ö¹º¯Êý²»¿ÉÄܱ»µ÷Óá£ÔÚÕâÖÖÇé¿öÏ£¬Î´ÊͷŵÄ×ÊԴͨ³£ÓɲÙ×÷ϵͳÀ´
´¦Àí¡£
* JavaµÄÎÞÓõ¥ÔªµÄÊÕ¼¯·¢ÉúµÄʱ¿ÌÊDz»È·¶¨µÄ£¬Òò´Ë£¬JavaÎÞ·¨±£Ö¤
ÔÚºÎʱµ÷ÓÃÖÕÖ¹º¯Êý¡¢ÖÕÖ¹º¯ÊýµÄµ÷ÓôÎÐò¡¢ÒÔ¼°ÓÉÄÄÒ»¸öÏß³ÌÖ´ÐÐÖÕ
Ö¹º¯Êý¡£
* ÔÚÖÕÖ¹º¯Êý±»µ÷ÓÃʱ£¬¶ÔÏó»á²»Á¢¿Ì±»»ØÊÕ¡£ÒòΪÖÕÖ¹º¯ÊýҪʹÓöÔ
ÏóµÄthisÖ¸Õë×öijЩ´¦Àí£¬Òâζ×ŶÔÏóÓÖ±»ÒýÓÃÁË¡£Ö»ÓÐÔÚfinalize()Ö´
ÐÐÖ®ºó£¬ÎÞÓõ¥ÔªÊÕ¼¯³ÌÐò²ÅÓпÉÄÜ´¦Àí¸Ã¶ÔÏó¡£
* ÉÏÃæÀý×ÓÖУ¬ÖÕÖ¹º¯Êý¿ÉÒÔÒý·¢Ò»¸öÒì³£¡£µ«ÊÇÈç¹ûÔÚÖÕÖ¹º¯ÊýÖз¢
ÉúÁËÆäËüÒì³££¬½«±»ÏµÍ³ºö¡£
4.6.3 nullÒýÓÃ
¹Ø¼ü×ÖnullÊÇÒ»¸öÔ¤¶¨ÒåµÄ³£Á¿£¬±íʾ¡°ÎÞʵÀý¡±¡£null¿ÉÒÔÓÃÔÚ
ʵÀýÄܹ»³öÏÖµÄÈκεط½£¬¿ÉÒÔ±»×ª»»³ÉÈκÎÀàÀàÐÍ¡£
4.7 ¾²Ì¬·½·¨£¬±äÁ¿ºÍ³õʼ»¯
±äÁ¿ºÍ·½·
--
¡ù À´Ô´:£®×϶¡Ïã pclinux.hit.edu.cn£®[FROM: zzz.hit.edu.cn]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
Ò³ÃæÖ´ÐÐʱ¼ä£º213.015ºÁÃë