¥»¤å±¦VJavaªì¾ÇªÌ¡A§Ú̦b±Ð§÷¤W¸g±`·|¬Ý¨ìJava¬O¦hºAªº¡A¥i¬O¤@¯ëªº±Ð¬ì®Ñ¥u°±¯d¦b²z½×¤W¡A«Ü¤Ö¦³¹ê»Ú¾Þ§@¡C²{¦b§â¦hºAÓ·§©À¥Î¥N½X²³æ´yz¤@¤U¡A¥Ñ©ó¥»¤H¤ô·Ç¦³¡A©Ò¥HÃø§K¦³¤£¨¬¤§³B¡C º¥ý§Ú̦³³o¼Ë´XÓÃþ¡A¹q¸£¡AÓ¤H¹q¸£¡Aµ§°O¥»¹q¸£¡C
class Computer{ public void turnOn(){ } } |
²{¦b¦³¤F¹q¸£³oÓÃþ¡A¨º»òÓ¤HPC¡Aµ§°O¥»³£¬OÄ~©Ó¥L¡C
class PC extends Computer{ public void turnOn(){ System.out.println("PC has turn on"); } } class NB extends Computer{ public void turnOn(){ System.out.println("NB has turn on"); } } |
§Ú̬ݨì¡A¨CÓÃþ³£¦³¤@Ó turnOn()¤èªk¡A¦Ó¦b¤÷Ãþ¤¤³oÓ¤èªk¬OªÅªº¡A¤lÃþ¤¤¦U¦Û¦³¤£¦Pªº¹ê²{¡A¨ì³o¸Ì¥i¯àÁÙ¤£¤Ó©úÅã¡C¦pªG¦³¥t¥~¤@Ó¤èªk¡An±µ¨ü³o¨ÇÃþ§@¬°°Ñ¼Æ¡A´N¹³³o¼Ë
class Test{ public void go(Computer computer){ computer.turnOn(); } } |
³oÓ¤èªk±µ¦¬ªº¬O°òÃþ°Ñ¼Æ¡A·íµM§Ú̬O¥i¥H§â¤lÃþ¶Ç»¼µ¹¤èªkªº¡A¤]À³¸Ó³o»ò°µ¡C
go(new PC()); go(new NB()); |
³o¼Ë¥L´N·|¥h½Õ¥Î¨ãÅé¤lÃþªºturnOn()¤èªk¡A©Ò¥H³o¨â¦¸½Õ¥Îªº¿é¥X¬O¤£¤@¼Ëªº.¦pªG¤£¥Î°òÃþ§@¬°°Ñ¼Æ,§ÚÌ·Q½Õ¥Î¨CÓ¤lÃþªºturnOn()¤èªk¡A¨º»ò´Nn¨Ç¨âÓ¤èªkÅé¤@¼Ëªºgo()¦Ó±µ¦¬¤£¦Pªº°Ñ¼Æªº«¸ü¡C
public void go(PC computer){ computer.turnOn(); } public void go(NB computer){ computer.turnOn(); } |
³o¼Ë¨Ç¤]¬O¥¿½Tªº¡A¦ý¬O³o¼Ë·|²£¥Í«Ü¦h«´_¥N½X¡A¦Ó¥B¤]¤£©öºûÅ@¡A·íµM³o¥u¬O¦hºA¤¤³Ì°ò¥»ªº¤@Ó³¡¤À¡AJava¦hºA¾÷¨îÁÙ¦³«Ü¦h¯S©Ê¡AÃö©ó³o¨Ç«Øij¤j®a¬Ý¬Ý java½sµ{«ä·Q(think in java)³o¥»®Ñ¡AùرÁ¿ªº«Ü¸Ô²Ó¡A³o¸Ì¥u¬O«Ü²³æªº´yz¤F¤@¤U¡AÁÙ±æÓ¦U¦ì°ª¤â¤£n¨£¯º¡C
http://big5.ccidnet.com:89/gate/big5/java.ccidnet.com/art/3539/20080903/1562769_1.html