15-1 ����ɦV�{���]�p

ª«¥ó¾É¦Vµ{¦¡³]­p¡]Object Oriented Programming¡A²ºÙ OOP¡^¬O¥Ø«e³nÅé³]­pªº¼é¬y¡A¤@¯ë°ª¶¥µ{¦¡»y¨¥³£¤ä´© OOP ªº·§©À¡A¨Ò¦p C++¡BJavaScript¡BC#¡BJava µ¥µ¥¡AMATLAB¤]¤£¨Ò¥~¡A¦]¦¹¥»³¹±N»¡©ú MATLAB ¦b OOP ·§©À¤Uªº¼¶¼g¤è¦¡¡A¥H«K²¤Æ³nÅ骺©â¶H·§©À¡B´£°ªµ{¦¡½Xªº¥iŪ©Ê»P­«½Æ¨Ï¥Î©Ê¡A¨ÃÅý³nÅ骺ºÞ²z§ó¥[²¼ä³z©ú¡C

­º¥ý¡A§Ú­Ì­n¯à°÷«Ø¥ß¤@­ÓÃþ§O¡]Class¡^¡A¨Ã¸g¥Ñ¦¹Ãþ§O«Øºc¥X¹ê»Úªºª«¥ó¡]Object¡^¡A¦p¦¹¤~¯à¶i¦Ó¨Ï¥Îª«¥óªº¦UºØ¤èªk¨Ó¹F¦¨À³¥Îµ{¦¡ªº³Ì«á¥Øªº¡CÃþ§O¬O¤@­ÓµêÀÀªº·§©À¡A¨Ò¦p¡u¤H¡v¥i¥H¬Ý¦¨¬O¤@­ÓÃþ§O¡A¥]§t¦UºØ©Ê½èªº©w¸q¡]¨Ò¦p¦W¦r¡B©Ê§O¡B¨­°ª¡BÅé­«µ¥¡^¡A¦ý¬Oª«¥ó«h¬O¹ê»Ú¥ÑÃþ§O²£¥Íªº¹êÅé¡A¨Ò¦p¡u¤Õ¤l¡v©Î¬O¡uª÷«°ªZ¡v´N¬O¥Ñ¡u¤H¡v©Ò²£¥Íªº¹êÅé¡A¦¹¹êÅé´N·|¦³½T¤Áªº¦W¦r¡B©Ê§O¡B¨­°ª¡BÅé­«µ¥©Ê½è¡C

¥H¤U§Ú­Ì±N¥H¥ý¦h¶µ¦¡¬°½d¨Ò¡A¨Ó»¡©ú MATLAB ¦p¦ó¹ê§@ OOP ªº°ò¥»·§©À¡CMATLAB «Ø¥ßÃþ§Oªº¤è¦¡¬Oª½±µ²£¥Í¤@­Ó¥H¡u@¡v¬°¶}ÀYªº¸ê®Æ§¨¡A©Ò¦³»P¦¹Ãþ§O¬ÛÃöªº¨ç¦¡³£©ñ¦b¦¹¸ê®Æ§¨¤§¤U¡A¨Ò¦p¡A¦b¥»³¹ªº½d¨Òµ{¦¡¥Ø¿ý¤¤¡A§A¥i¥Hµoı¦³¤@­Ó¥Ø¿ýªº¦WºÙ¬O @polynom¡A³o´N¬O¹ïÀ³¨ì¦h¶µ¦¡Ãþ§Oªº¥Ø¿ý¡A©Ò¦³¬ÛÃöªº¨ç¦¡¡]¤]´N¬Oª«¥óªº¤èªk¡^¤]³£©ñ¦b³o­Ó¥Ø¿ý¤U¡C

Hint
§A¥²¶·±N¥]§t¦¹ @polynom ªº¤W¼h¥Ø¿ý¥[¨ì MATLAB ªº·j´M¸ô®|«á¡A¤~¥i¥H¦b¥ô·N³B¨Ï¥Î»P polynom ¬ÛÃöªºª«¥ó©Î¤èªk¡C

±µµÛ§Ú­Ì­n©w¸q«Øºc¨ç¦¡¡]Constructor¡^¥H«K²£¥Í¦h¶µ¦¡ª«¥ó¡A¦¹«Øºc¨ç¦¡ªº¦WºÙ¥²¶·©MÃþ§Oªº¥Ø¿ý¦WºÙ¤@¼Ë¡]¦ý¤£¥]§t ¡§@¡¨¡^¡A¥H¦h¶µ¦¡ª«¥ó¦Ó¨¥¡A«Øºc¨ç¦¡ªº¦WºÙ´N¬O polynom.m¡A¨ä¤º®e¦p¤U¡G

Example 1: 15-ª«¥ó¾É¦Vµ{¦¡³]­p/@polynom/polynom.mfunction poly = polynom(vec) %POLYNOM Polynomial class constructor % poly = POLYNOM(vec) creates a polynomial object from the given vector vec % which contains the coefficients of the descending-order polynomial. if isa(vec, 'polynom') % ­Y vec ¤w¸g¬O polynom ª«¥ó¡A«hª½±µ³]©w¦¨¿é¥X poly = vec; else poly.c = vec(:).'; % ±N¦V¶q³]©w¦¨ poly ªº«Y¼Æ poly = class(poly, 'polynom'); % ±N poly ¥[«ù¦¨ polynom ª«¥ó end

±q¤W­zµ{¦¡½X¥i¥H¬Ý¥X¡G

¦pªG¿é¤J¬O¤@­Ó¦V¶q¡A³o´N¬O¦h¶µ¦¡ªº«Y¼Æ¡]­°¾­±Æ¦C¡^¡A§Ú­Ì±N¦¹«Y¼Æ³]©wµ¹ poly.c¡A³Ì«á¨Ï¥Î class ¨ç¦¡±N poly Åܼƥ[«ù¦¨ polynom ª«¥ó¡C

¦pªG¿é¤J vec ¤w¸g¬O¤@­Ó polynom ª«¥ó¡A«hª½±µ¿é¥X¦¹ª«¥ó¡C¡]isa(vec, ¡¥polynom¡¦) ¥Î¨ÓÀˬd vec ¬O§_¬O¤@­Ó polynom ª«¥ó¡C¡^

¥Ñ¤W­zµ{¦¡½X¤]¥i¥H¬Ý¥X¡Apoly ­ì¨Ó¬O¤@­Óµ²ºcÅܼơA¨ã¦³¤@­ÓÄæ¦ì c ¨ÓÀx¦s¦h¶µ¦¡ªº«Y¼Æ¡A¦ý¬O¤@¥¹¸g¥Ñ poly = class(poly, ¡¥polynom¡¦) ¨Ó±N¤§¥[«ù¦¨¬°ª«¥ó¤§«á¡A§Ú­Ì¦b¥~³¡¨ÃµLªk¸g¥Ñ poly ª«¥ó¨Ó¨ú±o c Äæ¦ì­È¡A¦Ó¥²¶·©w¸q¨ä¥L¤èªk¨Ó¨ú±o¸ê®Æ¡]¸Ô¨£«á­z¡^¡A³o¤]¬O OOP ªº°ò¥»ºë¯«¡G©Ò¦³ª«¥óªº©Ê½è¬OµLªk¥ô·N¶}©ñµ¹¥~¬ÉÀô¹Ò¡A¦Ó¥²¶·¸g¥Ñ»P¦¹ª«¥ó¬ÛÃöªº¯S©w¨ç¦¡¨Ó¨ú±o¡C

¦³¤F¦¹«Øºc¨ç¦¡¡A§Ú­Ì´N¥i¥H«Øºc¤@­Ó¦h¶µ¦¡ª«¥ó¡A¦ý¬°¤F¤ñ¸û¯à°÷Àu¶®¦a®i¥Ü¦¹ª«¥óªº¤º®e¡A§Ú­Ì¥i¥H¼g¤@­ÓÅã¥Ü¨ç¦¡ display.m ¨ÓÅã¥Ü¦¹¦h¶µ¦¡¡A¦¹Åã¥Ü¨ç¦¡ªº¦WºÙ¥²¶·©T©w¬° display.m¡A¥B¥²¶·¦ì©ó @polynom ¥Ø¿ý¤U¡A¤@¥¹ MATLAB »Ý­nÅã¥Ü¦¹¦h¶µ¦¡ª«¥ó¡A§Y·|¦Û°Ê©I¥s¦¹¨ç¦¡¡A¨ä¤º®e¦p¤U¡G

Example 2: 15-ª«¥ó¾É¦Vµ{¦¡³]­p/@polynom/display.mfunction display(poly) % POLYNOM/DISPLAY Display of a polynom disp(' '); disp([inputname(1),' = ']) disp(' '); disp([' ', polyAsString(poly)]) disp(' ');

¤W­z¨ç¦¡©I¥s¤F¥t¤@­Ó¨ç¦¡ polyAsString.m¡A¨ä¥\¯à¬O±N¦h¶µ¦¡Âର¦r¦ê§Î¦¡¡A«K©óÆ[¬Ý¡Aµ{¦¡½X¦p¤U¡G

Example 3: 15-ª«¥ó¾É¦Vµ{¦¡³]­p/@polynom/polyAsString.mfunction s = polyAsString(poly) % POLYNOM/POLYASSTRING String representation of a polynom degree=length(poly.c)-1; s = sprintf('%d*x^%d', poly.c(1), degree); for i=degree-1:-1:0 coef = poly.c(degree-i+1); if coef >=0 s=sprintf('%s + %d*x^%d', s, coef, i); else s=sprintf('%s - %d*x^%d', s, -coef, i); end end

¤@¥¹¦³¤F«Øºc¨ç¦¡ polynom.m ©MÅã¥Ü¨ç¦¡ display.m «á¡A§Ú­Ì´N¥i¥Hª½±µ²£¥Í¤@­Ó¦h¶µ¦¡ª«¥ó¨ÃÅã¥Ü¦¹ª«¥ó¡A¦p¤U¡G

Example 4: 15-ª«¥ó¾É¦Vµ{¦¡³]­p/test01.mp = polynom([3 4 2 1]) p = 3*x^3 + 4*x^2 + 2*x^1 + 1*x^0

¦b MATLAB ©R¥Oµøµ¡©Ò²£¥Íªº¦r¦ê¡A§Y¬O¥ÑÅã¥Ü¨ç¦¡ display.m ©Ò²£¥Íªºµ²ªG¡C¡]·íµM¡A³o­Ó¦r¦ê¥i¯à¤£¬O«Ü²Å¦X¤@¯ë¦h¶µ¦¡ªº¼gªk¡AŪªÌ¥i¥H¦Û¦æ­×§ï display.m¡A¨Ï¨ä²£¥Íªº¦r¦ê§óº}«G¤@¨Ç¡C¡^

Hint
¤W­zÅã¥Ü¨ç¦¡ÄÝ©ó¤@¯ëª«¥óªº¤º«ØÅã¥Ü¨ç¦¡¡A¨ä¥¦ªº»Pª«¥ó¬ÛÃöªº¨ç¦¡ÁÙ¦³¥[¡B´î¡B­¼¡B°£µ¥°ò¥»¹Bºâ¤¸¡A½Ð¨£¤U¤@¸`»¡©ú¡C

¥Ñ¨Ï¥ÎªÌ©Ò©w¸qªºÃþ§O¡A¨Æ¹ê¤W¬OÄÝ©ó MATLAB ¸ê®ÆÃþ§O¶¥¼hªº¤@³¡¥÷¡AMATLAB¸ê®ÆÃþ§O¶¥¼h¦p¤U¡G

¨ä¤¤ªº user classes ´N¬O¥Ñ¨Ï¥ÎªÌ©w¸qªºÃþ§O¡A¥¦Ä~©Ó¤F structure Ãþ§Oªº¬ÛÃö¯S©Ê¡C¥Ñ¤W­z¸ê®ÆÃþ§O¶¥¼h¡A§Ú­Ì¥i¥HÁA¸Ñ¸ê®ÆÃþ§O¤§¶¡ªº±qÄÝÃö«Y¡C

¤@¯ë¦Ó¨¥¡A¤ä´© OOP ªºµ{¦¡»y¨¥¤j²¤³£¦³¤U­z°ò¥»¥\¯à¡G

  1. ¨ç¦¡¤Î¹Bºâ¤¸ªº­«¸ü (Function and operator overloading)¡G®Ú¾Ú¯S©wªºÃþ§O©Îª«¥ó¡A§A¥i¥H²£¥Í¦UºØ¤èªk¡C¹ïÀ³³o¨Ç¤èªkªº¨ç¦¡¦WºÙ¥i¥H©M MATLABªº¤º«Ø¨ç¦¡¤@¼Ë¡A¦ýMATLAB·|®Ú¾Ú¿é¤J°Ñ¼Æªº¸ê®Æ«¬ºA¡A¨Ó¨M©w©I¥s­þ¤@ºØ¨ç¦¡¡C
  2. ¸ê®Æ©M¤èªkªº«Ê¸Ë (Encapsulation of data and methods)¡G©Ò¦³ª«¥óªº©Ê½è¡A¨ÃµLªk±q¥~¬Éª½±µ¨ú±o©Î­×§ï¡A¥²¶·¸g¥Ñ¦¹ª«¥ó©Ò´£¨Ñªº¤èªk¡A¤~¯à¨ú±o©Î­×§ï¦¹ª«¥óªº©Ê½è¡C³o¼Ë¥i¥H«OÃÒª«¥óªº§¹¾ã©Ê¡A¤£·|»~¾DŪ¨ú©Î­×§ï¡C¦¹¥~¡Aª«¥óªº¤èªk¤]¥u¾A¥Î©óª«¥ó¥»¨­¡A½T«O¤£·|»~¥Î¡C
  3. Ä~©Ó (Inheritance)¡GÃþ§O¥i¥H¦³Ä~©ÓÃö«Y¡A¨Ò¦p¡u¤H¡v¬O¤@ºØÃþ§O¡A¦Ó¡u¾Ç¥Í¡v«h¬O¡u¤H¡vªº¤lÃþ§O¡A¦]¦¹¡u¾Ç¥Í¡vÃþ§O¥i¥HÄ~©Ó¡u¤H¡vÃþ§Oªº©Ê½è©M¤èªk¡Cµ½¥ÎÄ~©Ó¡A¥iÅý§Ú­Ì¹ï¯u¹ê¥@¬Éªº´y­z§ó¥[§¹µ½¡C
  4. »E¦X (Aggregation)¡G§Ú­Ì¥i¥H¨Ï¥Î»E¦Xªº·§©À¨Ó©w¸q¬Y¨ÇÃþ§O¡A¸g¥Ñ³oºØÃþ§O©Ò²£¥Íªºª«¥ó¡A¥i¥H¥]§t¨ä¥Lª«¥ó¡C¨Ò¦p¡A¡u¾Ç¥Í¡v©Ò­×ªº¡u½Òµ{¡v¡A´N¥i¥H¥Ñ»E¦X¨Ó©w¸q¡C

¥H¤U´X¸`±N»¡©ú MATLAB ¹ï³o¨Ç OOP ¯S©Êªº¹ê§@©M½d¨Ò¡C


MATLABµ{¦¡³]­p¡G¶i¶¥½g