<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Macfeteria &#187; class</title>
	<atom:link href="http://www.macfeteria.com/blog/tag/class/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.macfeteria.com</link>
	<description>All About Mac and iPhone Programming</description>
	<lastBuildDate>Tue, 09 Aug 2011 04:34:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Objective-C Programmming :Extending Class</title>
		<link>http://www.macfeteria.com/blog/2008/11/04/objective-c-programmming-extending-class/</link>
		<comments>http://www.macfeteria.com/blog/2008/11/04/objective-c-programmming-extending-class/#comments</comments>
		<pubDate>Tue, 04 Nov 2008 07:23:20 +0000</pubDate>
		<dc:creator>[Ter]</dc:creator>
				<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[objective-c]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.macfeteria.com/?p=422</guid>
		<description><![CDATA[ปัญหาอย่างหนึ่งของการใช้ Framework หรือ Library ต่างๆก็คือ เราไม่อาจจะไปแก้ไขหรือเพ่ิมเติม method ต่างๆของ class ได้ เพราะว่าบางครั้ง Lib ที่ได้มานั้นก็มีแต่ lib ที่ผ่านการ compile เป็น binary files และมีแค่ header มาเท่านั้น วิธีการที่่เราจะเพิ่มเติมเข้าไปได้ก็คือ ต้องเขียน subclass ของ class นั้นๆ แต่ในภาษา Objective-C นั้นมีอีกวิธีหนึ่งที่เราสามารถเขียน method เพิ่มเติมเข้าไปยัง class เดิมได้ นั่นก็คือ category สมมติว่าเราต้องการเขียน โปรแกรมที่ต้องการ reverse string บ่อยๆ เราอาจจะเขียน class ขึ้นมาใหม่และเขียน method ที่ชื่อว่า getReverse ขึ้นมา อาจจะเขียนได้ประมาณนี้ ได้ว่า @interface ReverseString:NSObject &#123; NSString* [...]]]></description>
			<content:encoded><![CDATA[<p>ปัญหาอย่างหนึ่งของการใช้ Framework หรือ Library ต่างๆก็คือ เราไม่อาจจะไปแก้ไขหรือเพ่ิมเติม method ต่างๆของ class ได้ เพราะว่าบางครั้ง Lib ที่ได้มานั้นก็มีแต่ lib ที่ผ่านการ compile เป็น binary files และมีแค่ header มาเท่านั้น วิธีการที่่เราจะเพิ่มเติมเข้าไปได้ก็คือ ต้องเขียน subclass ของ class นั้นๆ แต่ในภาษา Objective-C นั้นมีอีกวิธีหนึ่งที่เราสามารถเขียน method เพิ่มเติมเข้าไปยัง class เดิมได้ นั่นก็คือ category</p>
<p>สมมติว่าเราต้องการเขียน โปรแกรมที่ต้องการ reverse string บ่อยๆ เราอาจจะเขียน class ขึ้นมาใหม่และเขียน method ที่ชื่อว่า getReverse ขึ้นมา อาจจะเขียนได้ประมาณนี้ ได้ว่า</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> ReverseString<span style="color: #002200;">:</span><span style="color: #400080;">NSObject</span>
<span style="color: #002200;">&#123;</span>
	<span style="color: #400080;">NSString</span><span style="color: #002200;">*</span> m_text;
<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> setString<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> text;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSMutableString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> getReverse;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>แล้วก็เขียน code ในส่วนของ getReverse ได้ว่า</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@implementation</span> ReverseString
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSMutableString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> getReverse
<span style="color: #002200;">&#123;</span>
	<span style="color: #400080;">NSMutableString</span> <span style="color: #002200;">*</span>temp <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableString</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span> ;
	unichar c;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> i <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>m_text length<span style="color: #002200;">&#93;</span><span style="color: #002200;">-</span><span style="color: #2400d9;">1</span> ; i <span style="color: #002200;">&amp;</span>gt;<span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span> ; i<span style="color: #002200;">--</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		c <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>m_text characterAtIndex<span style="color: #002200;">:</span>i<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>temp appendString<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableString</span> stringWithCharacters<span style="color: #002200;">:&amp;</span>c length<span style="color: #002200;">:</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">return</span> temp;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@end</span></pre></div></div>

<p>เขียนแบบนี้ก็พอได้ แต่ว่ามันก็มีข้อเสียคือ เมื่อเราต้องการจะ reverse string เราต้องให้ตัวแปร ของเราเป็น ReverseString</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">	ReverseString <span style="color: #002200;">*</span>hello <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>ReverseString alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>hello setString<span style="color: #002200;">:</span>@Hello<span style="color: #002200;">&#93;</span>;
	<span style="color: #400080;">NSMutableString</span> <span style="color: #002200;">*</span>revHello <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>hello getReverse<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>ซึ่งมันไม่สะดวก วิธีที่ดีกว่านั้นก็อาจจะเขียนเป็น function ขึ้นมาเป็น  global ก็พอได้</p>
<p>แต่ถ้าหากเราอยากจะทำให้ NSMutableString มี method ที่ชื่อว่า getReverse เลยจะทำได้ไม๊ ?<br />
คำตอบก็คือ ทำได้</p>
<p>Category นั้นทำให้เราสามารถที่จะเพิ่ม method ต่างๆเข้าไปยัง class ที่มีอยู่เดิมที่เราไม่สามารถจะไปแก้ไขในส่วนของ source ได้ แต่ Category ก็ไม่ได้สิทธิมากขนาดที่จะไปเพิ่ม member ใน Class ได้ เรามาลองดูการใช้ Category เลยอาจจะเข้าใจง่ายกว่า</p>
<p>จากตัวอย่างข้างบนนั้นถ้าหากเราต้องการจะเขียนให้  NSMutableString มี method นั้นเราก็ต้องเขียน category ขึ้นมาวิธีการ ก็สุดแสนจะง่ายดายเพียงแค่ โดยมีรูปแบบดังนี้</p>
<p>@interface ClassName ( CategoryName )</p>
<p>เอาละมาดูของจริงกันเลยดีกว่า ก่อนอื่นเราก็ประกาศ Category ที่ชื่อว่า MyExtend โดยเราจะเพิ่มเติมขยายในส่วนของ NSMutableString และเราก็เขียน method ใหม่ที่ชื่อว่า getReverse</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// MyString.h</span>
<span style="color: #a61390;">@interface</span> <span style="color: #400080;">NSMutableString</span> <span style="color: #002200;">&#40;</span>MyExtend<span style="color: #002200;">&#41;</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSMutableString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> getReverse;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>หลังจากนั้นก็เขียน ในส่วนของ source ได้แบบนี้</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// MyString.m</span>
<span style="color: #a61390;">@implementation</span> <span style="color: #400080;">NSMutableString</span> <span style="color: #002200;">&#40;</span>MyExtend<span style="color: #002200;">&#41;</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSMutableString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> getReverse
<span style="color: #002200;">&#123;</span>
	<span style="color: #400080;">NSMutableString</span> <span style="color: #002200;">*</span>temp <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableString</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span> ;
	unichar c;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> i <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self length<span style="color: #002200;">&#93;</span><span style="color: #002200;">-</span><span style="color: #2400d9;">1</span> ; i <span style="color: #002200;">&amp;</span>gt;<span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span> ; i<span style="color: #002200;">--</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		c <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self characterAtIndex<span style="color: #002200;">:</span>i<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>temp appendString<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableString</span> stringWithCharacters<span style="color: #002200;">:&amp;</span>c length<span style="color: #002200;">:</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">return</span> temp;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@end</span></pre></div></div>

<p>เพียงแค่เท่านี้ก็เสร็จเรียบร้อย หลังจากนั้นแล้ว NSMutableString ของเราก็จะมี method ใหม่ที่ชื่อว่า getReverse เรียบร้อยแล้ว</p>
<p>การเรียกใช้ก็ไม่ได้ยาก ดังตัวอย่าง</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;MyString.h&quot;</span>
&nbsp;
<span style="color: #a61390;">int</span> main <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> argc, <span style="color: #a61390;">const</span> <span style="color: #a61390;">char</span> <span style="color: #002200;">*</span> argv<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSAutoreleasePool</span> <span style="color: #002200;">*</span> pool <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSAutoreleasePool</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #400080;">NSMutableString</span> <span style="color: #002200;">*</span>text <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Hello&quot;</span>;
&nbsp;
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%@&quot;</span>,<span style="color: #002200;">&#91;</span>text getReverse<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#91;</span>pool drain<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">return</span> <span style="color: #2400d9;">0</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>และนอกจากนี้เรายังสามารถใช้ category เพื่อทำเป็น private method ได้ด้วย สมมติว่าเรามี Class ง่ายๆแบบนี้</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//AA.h</span>
<span style="color: #a61390;">@interface</span> AA <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span>
<span style="color: #002200;">&#123;</span>
<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span> getSomeInt;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>และเขียน implement ง่ายๆแบบนี้</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//AA.m</span>
<span style="color: #a61390;">@implementation</span> AA 
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span> getSomeInt
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">return</span> <span style="color: #2400d9;">10</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@end</span></pre></div></div>

<p>จะเห็นว่า  AA นั้นมี getSomeInt ซึ่งเป็น public method และในกรณีที่เราอยากให้มันเป็น private เราอาจจะดัดแปลงได้โดยการใช้ category ได้แบบนี้</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//AA.m</span>
<span style="color: #a61390;">@interface</span> AA<span style="color: #002200;">&#40;</span>hidden<span style="color: #002200;">&#41;</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> getHiddenInt;
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #a61390;">@implementation</span><span style="color: #002200;">&#40;</span>hidden<span style="color: #002200;">&#41;</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> getHiddenInt
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">return</span> <span style="color: #2400d9;">20</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> AA 
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span> getSomeInt
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">return</span> <span style="color: #2400d9;">10</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@end</span></pre></div></div>

<p>เป็นไงบ้างครับสำหรับการใช้ category จะเห็นว่ามันมีประโยชน์มากๆเลยสำหรับการขยายคลาส</p>

<!-- start wp-tags-to-technorati 1.02 -->

<p class='technorati-tags'>Technorati Tags: <a class='technorati-link' href='http://technorati.com/tag/class' rel='tag' target='_self'>class</a>, <a class='technorati-link' href='http://technorati.com/tag/objective-c' rel='tag' target='_self'>objective-c</a>, <a class='technorati-link' href='http://technorati.com/tag/programming' rel='tag' target='_self'>programming</a></p>

<!-- end wp-tags-to-technorati -->
]]></content:encoded>
			<wfw:commentRss>http://www.macfeteria.com/blog/2008/11/04/objective-c-programmming-extending-class/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Objective-C Programming Part IV</title>
		<link>http://www.macfeteria.com/blog/2008/03/09/objective-c-programming-part-iv/</link>
		<comments>http://www.macfeteria.com/blog/2008/03/09/objective-c-programming-part-iv/#comments</comments>
		<pubDate>Sun, 09 Mar 2008 12:43:33 +0000</pubDate>
		<dc:creator>[Ter]</dc:creator>
				<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[objective-c]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://maccafe.wordpress.com/?p=20</guid>
		<description><![CDATA[Super Self &#8211; Initializing หลังจากเรื่องที่แล้วได้เขียนเกี่ยวกับ Polymorphism ถ้าจะไม่พูดถึง super &#8211; self ก็คงจะไม่ครบสักเท่าไหร่ super เป็น keyword ที่เอาไว้อ้างอิงถึง คลาสที่สืบทอดมา self ก็คือเป็นการอ้างอิงถึงตัวเอง เอาละทีนี้ก็คือว่า มันมีความสำคัญอะไรกับ 2 สิ่งนี้ สำหรับ self กับ super จริงๆแล้ว self กับ super ส่วนมากจะได้ใช้จริงๆ กับ initial ซะส่วนมาก คือหลังจากการ alloc แล้วเราต้องทำการ init ให้มัน ทีนี้มันก็เกิดปัญหาที่ว่า ถ้าที่เราเขียน class ที่เป็น child จาก parent ที่ต้องการ initial ก่อนใช้งาน แล้วจะทำอย่างไร ? ตัวอย่าง Code ข้างล่างจะไม่มีปัญหา เพราะว่า [...]]]></description>
			<content:encoded><![CDATA[<h2>Super Self &#8211; Initializing</h2>
<p>หลังจากเรื่องที่แล้วได้เขียนเกี่ยวกับ Polymorphism ถ้าจะไม่พูดถึง super &#8211; self ก็คงจะไม่ครบสักเท่าไหร่</p>
<ul>
<li>super<br />
เป็น keyword ที่เอาไว้อ้างอิงถึง คลาสที่สืบทอดมา</li>
<li>self<br />
ก็คือเป็นการอ้างอิงถึงตัวเอง</li>
</ul>
<p>เอาละทีนี้ก็คือว่า มันมีความสำคัญอะไรกับ 2 สิ่งนี้ สำหรับ self กับ super<br />
จริงๆแล้ว self กับ super ส่วนมากจะได้ใช้จริงๆ กับ initial ซะส่วนมาก คือหลังจากการ alloc แล้วเราต้องทำการ init ให้มัน ทีนี้มันก็เกิดปัญหาที่ว่า ถ้าที่เราเขียน class ที่เป็น child จาก parent ที่ต้องการ initial ก่อนใช้งาน แล้วจะทำอย่างไร ?</p>
<p>ตัวอย่าง Code ข้างล่างจะไม่มีปัญหา เพราะว่า Child นั้นไม่ต้องการเขียน method ชื่อ init ดังนั้นก็ไปใช้ของ parent ได้</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &lt;Objc/Object.h&gt;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// ---- Children Class -----</span>
<span style="color: #a61390;">@interface</span> Children <span style="color: #002200;">:</span>Object 
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">int</span> m_age;
<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> init<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span> age;
<span style="color: #a61390;">@end</span>
&nbsp;
&nbsp;
<span style="color: #11740a; font-style: italic;">// ---- Student Class -----</span>
<span style="color: #a61390;">@interface</span> Student <span style="color: #002200;">:</span>Children 
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">float</span> m_studentID;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@end</span></pre></div></div>

<p>แบบนี้ก็อาจจะง่าย เวลาใช้งาน ก็อาจจะเขียนประมาณนี้</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">ChildClass<span style="color: #002200;">*</span> child <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>ChildClass alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">:</span><span style="color: #2400d9;">12</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>ก็ตอนนี้ก็เท่ากับว่า ค่า m_age นั้นมีค่าเท่ากับ 12</p>
<p>แต่ถ้าสมมติว่า initial ของ child ต้องการ parameter ด้วย เช่นว่า Children ก็ต้องการให้รับ age ตอน init<br />
และ Student ก็ต้องการให้มี studentID ในตอน init ด้วยละ ดังเช่นตัวอย่าง</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &lt;Objc/Object.h&gt;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// ---- Children Class -----</span>
<span style="color: #a61390;">@interface</span> Children <span style="color: #002200;">:</span>Object 
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">int</span> m_age;
<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> init<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span> age;
<span style="color: #a61390;">@end</span>
&nbsp;
&nbsp;
<span style="color: #11740a; font-style: italic;">// ---- Student Class -----</span>
<span style="color: #a61390;">@interface</span> Student <span style="color: #002200;">:</span>Children 
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">float</span> m_studentID;
<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> init<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">float</span><span style="color: #002200;">&#41;</span> studentID;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>เราจะเขียน code ในลักษณะแบบนี้</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">ChildClass<span style="color: #002200;">*</span> child <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>ChildClass alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">:</span><span style="color: #2400d9;">12.00</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>มันก็จะไปเรียก init ของ child และในตอนนี้ m_studentID ก็มีค่าเท่ากับ 12.00 แล้ว m_age ละ ? เราจะทำการ set ค่า initial สำหรับ parent class ได้อย่างไร ?<br />
เราจะรู้ได้อย่างไรว่า parent class นั้นมี member data อะไรอยู่ในนั้นบ้าง ?<br />
เพราะในขณะนี้เรา overriding ไปแล้ว มันก็เป็นการเรียกใช้ init ของ child โดยไม่ได้เรียก init ของ parent </p>
<p>เพื่อความเข้าใจ ลองมาดูตัวอย่างอีกสักอันกันดีกว่า</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Car.h</span>
<span style="color: #6e371a;">#import &lt;Objc/Object.h&gt;</span>
<span style="color: #a61390;">@interface</span> Car<span style="color: #002200;">:</span>Object <span style="color: #002200;">&#123;</span>
<span style="color: #a61390;">int</span> m_speed;
<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> init;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> setSpeed<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>speed;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span> getSpeed
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> Car;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> setSpeed<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span> speed
<span style="color: #002200;">&#123;</span>
	m_speed <span style="color: #002200;">=</span> speed;
<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> init
<span style="color: #002200;">&#123;</span>
	m_speed <span style="color: #002200;">=</span> <span style="color: #2400d9;">20</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span> getSpeed
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> m_speed;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@end</span></pre></div></div>

<p>แล้วเราก็เขียนในส่วนของ main แบบนี้</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;car.h&quot;</span>
<span style="color: #a61390;">int</span> main<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
	Car <span style="color: #002200;">*</span>honda <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>Car alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">printf</span><span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">&quot;Honda speed:&quot;</span>, <span style="color: #002200;">&#91;</span>honda getSpeed<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
	<span style="color: #a61390;">return</span> <span style="color: #2400d9;">0</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>ผลจากการ โปรแกรมก็จะได้ว่า</p>
<p>Honda speed: 20</p>
<p>ความจริงแล้วจาก code ข้างบนถ้าทำการ compile มันก็จะไม่แจ้ง error อะไร แต่มันจะแจ้งwarning ออกมา ว่า <span style="color: #ff0000;">warning: multiple methods named &#8216;-init&#8217; found</span> แต่จริงๆแล้วก็ทำงานได้ปกติ เหตุผลทีมันแจ้งแบบนี้ก็เพราะว่า init นั้นเป็น method ของ Object นั่นเอง และเราก็ทำการ Overriding มันไปนั่นเอง ทำให้มันเห็น ว่ามี init อยู่ 2 method</p>
<p>โอเคทีนี้สมมติว่าเรา อาจจะให้มี init กับ initWithSpeed ละ ? ตัวแรกไม่ต้องทำอะไร ส่วนตัวที่สอง ทำการ ใส่ speed เริ่มต้นให้เลย จะเขียนได้แบบไหน เราก็ได้ความคิดว่า งั้น init ก็ไม่ต้องไป Overriding ให้มัน แล้วเราก็เขียน initWithSpeed เพิ่มขึ้นมานั่นเอง โอเค งั้นมาดูเลย</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &lt;Objc/Object.h&gt;</span>
<span style="color: #a61390;">@interface</span> Car<span style="color: #002200;">:</span>Object 
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">int</span> m_speed;
<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> initWithSpeed<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span> speed;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> setSpeed<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>speed;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span> getSpeed;</pre></div></div>

<p>จะเห็นว่า เราก็แค่ เปลี่ยนจาก init ไปเป็น initWithSpeed เท่านั้นเอง งั้นส่วนของ implement นั้นก็ควรจะเป็น</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> initWithSpeed<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>speed
<span style="color: #002200;">&#123;</span>
	m_speed <span style="color: #002200;">=</span> speed;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>แบบนี้ก็เป็นอันจบ แล้วส่วน main ก็น่าจะเขียนแบบนี้</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">Car <span style="color: #002200;">*</span>honda <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>Car alloc<span style="color: #002200;">&#93;</span> initWithSpeed<span style="color: #002200;">:</span><span style="color: #2400d9;">20</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p><span style="color: #ff0000;"><strong>แต่ความเป็นจริงแล้ว มันไม่ได้ง่ายขนาดนั้น !!!!</strong></span></p>
<p>ถ้า compile จริงๆมันจะเกิด error ขึ้น เพราะว่า  initWithSpeed นั้น return void  โอเค เราจะแก้ปัญหานี้ได้โดยการเขียน code ใน main แบบนี้</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">Car<span style="color: #002200;">*</span>honda <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>Car alloc<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>honda initWithSpeed<span style="color: #002200;">:</span><span style="color: #2400d9;">20</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>แต่ความจริงแล้ว การแก้ปัญหาแบบนี้มันไม่ค่อยถูกเท่าไหร่ เพราะว่าถ้าจะพิจารณาให้ดีแล้ว มันเหมือนกับเราเขียนว่า initWithSpeed เป็นแค่ function ที่่เรียกใช้หลักจากการสร้าง honda แล้วมากกว่า ถ้าจะสรุปง่ายก็คือว่าการเขียนแบบข้างบน ก็เหมือนกับ การเขียนแบบนี้</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">Car <span style="color: #002200;">*</span>honda <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>Car alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>honda initWithSpeed<span style="color: #002200;">:</span><span style="color: #2400d9;">20</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>เอาละ งั้นเราจะเขียนให้มันใช้ initWithSpeed ร่วมกับ alloc ได้ยังไง</p>
<p>ไม่ยากเลย  เราก็ให้ initWithSpeed ไม่ต้อง return void ให้มัน return Car ออกแทนสิ แต่จะให้มัน return ได้ยังไง นี่คือปัญหา  และ self กับ super จะได้เอามาใช้ตอนนี้เหละ งั้นก็ดูกันเลย</p>
<p>เราทำการเปลี่ยน ในส่วนของ interface ก่อน</p>
<p>โดยเราทำการเปลี่ยนให้ initWithSpeed ทำการ return id ออกมาแทน</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> initWithSpeed<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span> speed;</pre></div></div>

<p>และในส่วนของ implementation นั้น จะเขียนได้แบบนี้</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> initWithSpeed<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span> speed;
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>super init<span style="color: #002200;">&#93;</span>;
	m_speed <span style="color: #002200;">=</span> speed;
	<span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>จะเห็นว่า เราทำการ เรียกใช้ super และ self</p>
<ul>
<li>[super init]<br />
ในส่วนนี้เป็นการบอกว่า ให้ เรียกใช้ Class แม่ (super) และทำการ เรียกใช้ init เพราะว่า จริงๆแล้ว Car นั้นสืบทอดมาจาก Object และ init นั้นก็เป็น method ที่เรียกใช้เวลา ทำการ  alloc ตัว object ก็เป็นอันว่า เราได้ทำการ init เรียบร้อยแล้ว ต่อไปก็</li>
<li>m_speed = speed<br />
ตรงนีก็ไม่ได้มีอะไรพิเศษนอกจากทำการ ใส่ค่าให้กับตัวแปร m_speed</li>
<li>return self<br />
ตรงนี้สำคัญเพราะเมื่อเราทำการ initตัวแม่แล้ว และเราก็ทำการ ใส่ค่าให้กับตัวแปรที่ต้องการแล้ว แน่นอนว่า การ alloc นั้น ต้องการขอจอง memory สำหรับตัวมันเอง ไม่ใช่ตัวแม่ ดังนั้นเราก็ return ตัวมันเองออกไป</li>
</ul>
<p>และทีนี้เราก็จะเขียน main โปรแกรมได้ใหม่ว่า</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;car.h&quot;</span>
<span style="color: #a61390;">int</span> main<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
	Car <span style="color: #002200;">*</span>honda <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>Car alloc<span style="color: #002200;">&#93;</span> initWithSpeed<span style="color: #002200;">:</span><span style="color: #2400d9;">50</span> <span style="color: #002200;">&#93;</span>;
	Car <span style="color: #002200;">*</span>toyota <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>Car alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">printf</span><span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">&quot;Honda speed:&quot;</span>, <span style="color: #002200;">&#91;</span>honda getSpeed<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
	<span style="color: #a61390;">printf</span><span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">&quot;Honda speed:&quot;</span>, <span style="color: #002200;">&#91;</span>toyota getSpeed<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
	<span style="color: #002200;">&#91;</span>honda release<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>toyota release<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">return</span> <span style="color: #2400d9;">0</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>แบบนี้ก็จะไม่เกิด error หรือ warning ใดๆ เลย และถ้าไม่อยาก initWithSpeed ก็ทำได้เหมือนกัน</p>
<p>ผลจากการ run โปรแกรมก็จะออกมาได้ว่า</p>
<p>Honda speed: 50<br />
Toyota speed: 0</p>
<p>ลองโหลด source ไปลองดูได้ครับ</p>
<div><a href="http://www.macfeteria.com/wp-content/uploads/2008/07/objc_part_iv.zip">Download Objective-C Programming Part IV Source Files</a></div>

<!-- start wp-tags-to-technorati 1.02 -->

<p class='technorati-tags'>Technorati Tags: <a class='technorati-link' href='http://technorati.com/tag/class' rel='tag' target='_self'>class</a>, <a class='technorati-link' href='http://technorati.com/tag/objective-c' rel='tag' target='_self'>objective-c</a>, <a class='technorati-link' href='http://technorati.com/tag/programming' rel='tag' target='_self'>programming</a></p>

<!-- end wp-tags-to-technorati -->
]]></content:encoded>
			<wfw:commentRss>http://www.macfeteria.com/blog/2008/03/09/objective-c-programming-part-iv/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Objective-C Programming Part III.</title>
		<link>http://www.macfeteria.com/blog/2008/03/03/objective-c-programming-part-iii/</link>
		<comments>http://www.macfeteria.com/blog/2008/03/03/objective-c-programming-part-iii/#comments</comments>
		<pubDate>Sun, 02 Mar 2008 19:26:01 +0000</pubDate>
		<dc:creator>[Ter]</dc:creator>
				<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[advance]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[objective-c]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://maccafe.wordpress.com/?p=15</guid>
		<description><![CDATA[Polymorphisms Polymorphisms สรุปง่ายๆว่าเป็นการอนุญาติให้ derived class ( child class ) สามารถใช้ function ของ parent ได้ สำหรับ polymorphisms นั้นเห็นเด่นชัดมากที่สุดก็คงจะเป็น Overriding / Overloading Overriding เป็นการเขียน function ของ Child class ใช้เองโดยไม่ต้องใช้ function ของ Parent Class ถ้าจะยกตัวอย่างง่ายๆ ก็เป็นต้นว่า ถ้าเรามี class ชื่อว่า Car แล้ว Car นั้นมีฟังชั่นขื่อว่า drive สมมติว่าเราจะเขียน class ใหม่ขึ้นมาโดยสืบทอด (inherit) มาจาก Car โดยชื่อว่า Tank มีฟังชั่นชื่อว่า drive เหมือนกัน แต่ว่า class ทั้งสองนั้น [...]]]></description>
			<content:encoded><![CDATA[<h2>Polymorphisms</h2>
<p>Polymorphisms สรุปง่ายๆว่าเป็นการอนุญาติให้ derived class ( child class ) สามารถใช้ function ของ parent ได้ สำหรับ polymorphisms นั้นเห็นเด่นชัดมากที่สุดก็คงจะเป็น Overriding / Overloading</p>
<ul>
<li><strong>Overriding</strong><br />
เป็นการเขียน function ของ  Child class ใช้เองโดยไม่ต้องใช้ function ของ Parent Class  ถ้าจะยกตัวอย่างง่ายๆ ก็เป็นต้นว่า ถ้าเรามี class ชื่อว่า Car แล้ว Car นั้นมีฟังชั่นขื่อว่า drive  สมมติว่าเราจะเขียน class ใหม่ขึ้นมาโดยสืบทอด (inherit) มาจาก Car โดยชื่อว่า Tank มีฟังชั่นชื่อว่า drive เหมือนกัน แต่ว่า class ทั้งสองนั้น Drive ทำงานไม่เหมือนกัน  คือพูดง่ายๆว่า รถยนต์ก็<span style="color: #a0410d;"> ขับ(drive)</span>ได้ รถถังก็<span style="color: #a0410d;"> ขับ(drive)</span> ได้ แต่วิธีการที่จะขับไม่เหมือนกัน</li>
<li><strong>Overloading</strong><br />
เป็นการเขียน Function ชื่อเหมือนกัน ใน class เดียวกัน แต่รับ parameter คนละอย่างกัน เช่นว่า เรามี class ชื่อว่า Rectangle และมี function ในการคำนวนพื้นที่ เราอาจจะต้องการให้มันรับ parameter ได้ทั้ง int และ float ในลักษณะแบบนี้เราก็จะใช้ overloading เข้ามาให้เกิดประโยชน์</li>
</ul>
<p>อาจจะงง เล็กน้อย สรุปสั้น Overriding ก็คือ child class ที่มีการเขียน function ที่ชื่อเหมือนกับ parent ขึ้นมาเองโดยไม่ต้องสืบทอดจาก parent และ Overloading คือ method ที่สามารถรับ parameter ได้หลายๆรูปแบบ </p>
<p>แต่มาดู code อาจจะเข้าใจง่ายกว่า</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Car.h</span>
<span style="color: #6e371a;">#import </span>
<span style="color: #a61390;">@interface</span> Car<span style="color: #002200;">:</span>Object <span style="color: #002200;">&#123;</span>
<span style="color: #a61390;">int</span> m_speed;
<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> drive;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> drive<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>speed;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>และในส่วน implement</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Car.m</span>
<span style="color: #6e371a;">#import </span>
<span style="color: #a61390;">@implementation</span> Car;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> drive
<span style="color: #002200;">&#123;</span>
<span style="color: #a61390;">printf</span><span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">&quot;Car drive n&quot;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> drive<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>speed
<span style="color: #002200;">&#123;</span>
m_speed <span style="color: #002200;">=</span> speed;
<span style="color: #a61390;">printf</span> <span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">&quot;Car drive at speed %d n&quot;</span>,m_speed<span style="color: #002200;">&#41;</span>; <span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@end</span></pre></div></div>

<p>จากตัวอย่างข้างบนเป็นการ แสดงให้เห็นถึง Overloading คือ จะเห็นว่า Car นั้นมี Function ชื่อเหมือนกัน นั่นคือ drive แต่รับ parameter มาไม่เหมือนกัน โดยที่ drive แรกนั้น ไม่ได้รับ parameter มาเลย ส่วนตัวที่สอง รับ int เข้ามาและ ก็ในส่วนการทำงานของแต่ละฟังชั่นก็ต่างกัน</p>
<p>เวลาเรียกใช้เราอาจจะเขียนได้ว่า</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;car.h&quot;</span>
<span style="color: #a61390;">int</span> main<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
Car <span style="color: #002200;">*</span>honda <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>Car alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>honda drive<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>honda drive<span style="color: #002200;">:</span><span style="color: #2400d9;">80</span><span style="color: #002200;">&#93;</span>;
<span style="color: #a61390;">return</span> <span style="color: #2400d9;">0</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>ส่วนผลลัพธ์ที่ออกมาก็จะเป็น</p>
<pre>Car drive
Car drive at speed 80</pre>
<p>ทีนี้เรามาดู Overriding กันบ้าง ก็อย่างที่บอกไปแล้วว่า เราสารมารถเขียน ให้ child class ทำงานต่างจาก parent โดยที่ ขื่อเหมือนกันได้มาดู กันเลยดีกว่า</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Tank.h</span>
<span style="color: #6e371a;">#import &quot;car.h&quot;</span>
<span style="color: #a61390;">@interface</span> Tank<span style="color: #002200;">:</span>Car <span style="color: #002200;">&#123;</span>
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@end</span></pre></div></div>

<p>ก็จะเห็นว่าใน class tank นั้นไม่มีอะไรเลย คือ inherit มาจาก car อย่างเดียว แต่เราสารมารถเขียน implement ของตัวเองโดยอิงจาก parent ได้</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;tank.h&quot;</span>
<span style="color: #a61390;">@implementation</span> Tank
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> drive
<span style="color: #002200;">&#123;</span>
<span style="color: #a61390;">printf</span> <span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">&quot;Tank Driven&quot;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@end</span></pre></div></div>

<p>ก็เป็นอันเสร็จ คือเรา เขียน funtion drive ขึ้นมาใหม่ แทนที่ ตัวเก่าที่มีอยู่ใน Car</p>
<p>และก็มาลองเรียกใช้งานกันดู</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;car.h&quot;</span>
<span style="color: #6e371a;">#import &quot;tank.h</span>
<span style="color: #a61390;">int</span> main<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
Car <span style="color: #002200;">*</span>honda <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>Car alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
Tank <span style="color: #002200;">*</span>tank <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>Tank alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>honda drive<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>tank drive<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>tank drive<span style="color: #002200;">:</span><span style="color: #2400d9;">20</span><span style="color: #002200;">&#93;</span>;
<span style="color: #a61390;">return</span> <span style="color: #2400d9;">0</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>ส่วนผลลัพธ์ที่ได้ก็จะได้ว่า</p>
<pre>Car drive
Tank drive
Car drive at speed 20</pre>
<p>จากผลลัพธ์ก็จะเห็นว่า Tank drive ที่ออกมา โปรแกรมจะไปเรียก function ใน class Tank</p>
<p>ส่วนบรรทัดสุดท้าย ตัวโปรแกรมจะไปเรียกใช้ parent ของ tank นั่นก็คือ Car เพราะเนื่องจากว่า tank นั้น Overriding เพียงแค่ฟังชั่น</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> drive;</pre></div></div>

<p>แต่สำหรับ</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> drive<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>speed;</pre></div></div>

<p>ไม่ได้ทำการ overriding</p>
<p>มันจึงไปใช้งาน parent ของมันแทน</p>
<p>เพิ่มเติม <a href="http://gd.tuwien.ac.at/languages/c/c++oop-pmueller/">http://gd.tuwien.ac.at/languages/c/c++oop-pmueller/</a><br />
และ <a href="http://developer.apple.com/documentation/Cocoa/Conceptual/OOP_ObjC/Introduction/chapter_1_section_1.html">http://developer.apple.com/documentation/Cocoa/Conceptual/OOP_ObjC/Introduction/chapter_1_section_1.html</a></p>

<!-- start wp-tags-to-technorati 1.02 -->

<p class='technorati-tags'>Technorati Tags: <a class='technorati-link' href='http://technorati.com/tag/advance' rel='tag' target='_self'>advance</a>, <a class='technorati-link' href='http://technorati.com/tag/class' rel='tag' target='_self'>class</a>, <a class='technorati-link' href='http://technorati.com/tag/objective-c' rel='tag' target='_self'>objective-c</a>, <a class='technorati-link' href='http://technorati.com/tag/programming' rel='tag' target='_self'>programming</a></p>

<!-- end wp-tags-to-technorati -->
]]></content:encoded>
			<wfw:commentRss>http://www.macfeteria.com/blog/2008/03/03/objective-c-programming-part-iii/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Pure Virtual</title>
		<link>http://www.macfeteria.com/blog/2008/02/28/pure-virtual/</link>
		<comments>http://www.macfeteria.com/blog/2008/02/28/pure-virtual/#comments</comments>
		<pubDate>Thu, 28 Feb 2008 16:44:42 +0000</pubDate>
		<dc:creator>[Ter]</dc:creator>
				<category><![CDATA[Technic]]></category>
		<category><![CDATA[advance]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://maccafe.wordpress.com/?p=10</guid>
		<description><![CDATA[คือจาก เมื่อวันที่ผ่านมา เนื่องจากว่า งานที่ทำอยู่มีส่วนต้องให้ port โปรแกรมจาก Win มาสู่ mac และบังเอิญว่า ผม compile แล้วมันผ่าน แต่ว่าตอน Runtime มันดัน Error สิ่งที่มันบอกมาก็คือว่า pure virtual method called terminated called with out an active exception ปัญหาที่เกิดขึ้น ก็ยัง งงๆๆว่าเป็นเพราะอะไร แต่พอหลังจากให้เพื่อนมาดูแล้วก็นั่งถกกัน ว่ามันเกิด เฉพาะกับ mac หรือเปล่าเพราะว่าใน win ไม่เห็นมันจะเกิด ก็เลยทำการเขียน code ที่ทำงานแบบเดียวกัน ก็ัดังตัวอย่างข้างล่าง #include &#60;stdio.h&#62; class Base; void foo&#40;Base*&#41;; class Base &#123; public: Base&#40;&#41;&#123;&#125;; virtual [...]]]></description>
			<content:encoded><![CDATA[<p>คือจาก เมื่อวันที่ผ่านมา  เนื่องจากว่า งานที่ทำอยู่มีส่วนต้องให้ port โปรแกรมจาก Win มาสู่ mac และบังเอิญว่า ผม compile แล้วมันผ่าน แต่ว่าตอน Runtime มันดัน Error สิ่งที่มันบอกมาก็คือว่า  <span style="color: #808000;"><strong>pure virtual method called terminated called with out an active exception</strong></span> ปัญหาที่เกิดขึ้น ก็ยัง งงๆๆว่าเป็นเพราะอะไร แต่พอหลังจากให้เพื่อนมาดูแล้วก็นั่งถกกัน ว่ามันเกิด เฉพาะกับ mac หรือเปล่าเพราะว่าใน win ไม่เห็นมันจะเกิด ก็เลยทำการเขียน code ที่ทำงานแบบเดียวกัน ก็ัดังตัวอย่างข้างล่าง</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;stdio.h&gt;</span>
<span style="color: #0000ff;">class</span> Base<span style="color: #008080;">;</span>
<span style="color: #0000ff;">void</span> foo<span style="color: #008000;">&#40;</span>Base<span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">class</span> Base
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
		Base<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">virtual</span> ~Base<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			foo<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">this</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span>
		<span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> banana<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">void</span> foo<span style="color: #008000;">&#40;</span>Base<span style="color: #000040;">*</span> base<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	base<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>banana<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">class</span> Derived<span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> Base
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
		Derived<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span>
		~Derived<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span>
		<span style="color: #0000ff;">void</span> banana<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Banana&quot;</span><span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> argc, <span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span> <span style="color: #0000ff;">const</span> argv<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
&nbsp;
	Derived<span style="color: #000040;">*</span> test <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> Derived<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Hello, World!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
	<span style="color: #0000dd;">delete</span> test<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>ก็ถ้า ดูตาม code ก็จะเห็นว่า มี Base กับ Derived  <span style="color: #808000;"><strong>เนื่องจากว่า Base Class มันเป็น Pure Virtual (Abtract Class) เราต้อง implement ส่วนของ code การทำงานใน derived class เสมอไม่อาจจะเขียน implement ใน Base Class ได้  </strong></span> และปัญหาจากโปรแกรมข้างบนเกิดขึ้นเพราะในขณะที่ base class ได้เรียก destructor ของตัวเองนั้น มันกลับไปเรียกใช้งานฟังชั่นใน derived class ด้วย ซึ่งนั่นก็ทำให้มันเกิด error เพราะว่า derived class ได้ถูกทำลายไปก่อนแล้ว  ทำให้ไม่สามารถเรียกได้</p>
<p>ลองดูตรง</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">virtual</span> ~Base<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	foo<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">this</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>จาก destructor นี้จะเห็นว่า มีการเรียกฟังก์ชั่น foo และแน่นอนว่า foo ไปเรียก</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">base<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>banana<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>อีกที สืบเนื่องจากว่ามันเป็น pure virtual แปลว่าตัว Base Class เองไม่อาจจะเขียน implement code ได้มันจึงต้องไปเรียก banana ในตัว Derived Class ที่มีการเขียน implement code และ Derived นั้นได้ทำลายจาก destructor ไปเรียบร้อยแล้ว มันเลยไม่เจอ banana ทำให้เกิด error นั่นเอง</p>
<p>ลองศึกษา เพิ่มเติมเกี่ยวกับ Virtual  Function ได้ที่ <a title="Vitual Function" href="http://en.wikipedia.org/wiki/Virtual_function">http://en.wikipedia.org/wiki/Virtual_function</a></p>

<!-- start wp-tags-to-technorati 1.02 -->

<p class='technorati-tags'>Technorati Tags: <a class='technorati-link' href='http://technorati.com/tag/advance' rel='tag' target='_self'>advance</a>, <a class='technorati-link' href='http://technorati.com/tag/c%2B%2B' rel='tag' target='_self'>c++</a>, <a class='technorati-link' href='http://technorati.com/tag/class' rel='tag' target='_self'>class</a>, <a class='technorati-link' href='http://technorati.com/tag/programming' rel='tag' target='_self'>programming</a></p>

<!-- end wp-tags-to-technorati -->
]]></content:encoded>
			<wfw:commentRss>http://www.macfeteria.com/blog/2008/02/28/pure-virtual/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Objective-C Programming II : More about Class &#8211; Inheritance</title>
		<link>http://www.macfeteria.com/blog/2008/02/24/more-about-class-inheritance/</link>
		<comments>http://www.macfeteria.com/blog/2008/02/24/more-about-class-inheritance/#comments</comments>
		<pubDate>Sat, 23 Feb 2008 18:57:53 +0000</pubDate>
		<dc:creator>[Ter]</dc:creator>
				<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[basic]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[objective-c]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://maccafe.wordpress.com/?p=8</guid>
		<description><![CDATA[สำหรับหัวข้อนี้จะกล่าวถึงรายละเอียดต่างๆเกี่ยวกับ Inheritance (การสืบทอด) ว่ามันคืออะไรและมีความสำคัญยังไง ตั้งแต่พื้นๆก่อนเลยละกัน ก่อนอื่นมาทำความเข้าใจ ของความหมาย ของคำว่า Root class ความหมายของ root คือ class ที่ไม่มี parent หรือหมายความง่ายๆว่ามันไม่ได้ไปสืบทอดใคร Parent class ส่วน parent นั้นก็คือ class ที่เป็นต้นแบบของ class อื่นๆ และตัว parent เองก็อาจจะสืบทอดคนอื่น(child) มาอีกทีก็ได้ Child class เป็นการเรียก class ที่สืบทอดมาจากคนอื่น เพื่อความเข้าใจง่ายๆ ยกตัวอย่าง เรื่องของสัตว์มาประกอบ ก็แล้วกันน่ะสัตว์ทั้งหมดในโลก ขอเรียกว่า เป็น Animal ทั้งหมดเลยแล้วกัน แล้วสัตว์ต่างๆ ก็อาจจะแบ่งออกได้เป็น Mammal ( สัตว์เลี้ยงลูกด้วยนม ) และ Fish ( ปลา ) และ [...]]]></description>
			<content:encoded><![CDATA[<p>สำหรับหัวข้อนี้จะกล่าวถึงรายละเอียดต่างๆเกี่ยวกับ Inheritance (การสืบทอด) ว่ามันคืออะไรและมีความสำคัญยังไง ตั้งแต่พื้นๆก่อนเลยละกัน ก่อนอื่นมาทำความเข้าใจ ของความหมาย ของคำว่า</p>
<ul>
<li>Root class<br />
ความหมายของ root คือ class ที่ไม่มี parent หรือหมายความง่ายๆว่ามันไม่ได้ไปสืบทอดใคร</li>
<li>Parent class<br />
ส่วน parent นั้นก็คือ class ที่เป็นต้นแบบของ class อื่นๆ และตัว parent เองก็อาจจะสืบทอดคนอื่น(child) มาอีกทีก็ได้</li>
<li>Child class<br />
เป็นการเรียก class ที่สืบทอดมาจากคนอื่น</li>
</ul>
<p>เพื่อความเข้าใจง่ายๆ ยกตัวอย่าง เรื่องของสัตว์มาประกอบ ก็แล้วกันน่ะสัตว์ทั้งหมดในโลก ขอเรียกว่า เป็น Animal ทั้งหมดเลยแล้วกัน แล้วสัตว์ต่างๆ ก็อาจจะแบ่งออกได้เป็น Mammal ( สัตว์เลี้ยงลูกด้วยนม ) และ Fish ( ปลา ) และ ถ้าในบรรดาเหล่า Mammal ก็มี Dog ซึ่งเป็นสัตว์ชนิดหนึ่งเหมือนกัน<br />
เพื่อความเข้าใจง่ายๆ ดูภาพประกอบ</p>
<p style="text-align: center;"><a href="http://www.macfeteria.com/wp-content/uploads/2008/08/diagram1.gif" rel="lightbox[62]"><img class="size-medium wp-image-325 aligncenter" title="diagram" src="http://www.macfeteria.com/wp-content/uploads/2008/08/diagram1-300x165.gif" alt="" width="300" height="165" /></a></p>
<p>อธิบายตามภาพ</p>
<ul>
<li>Animal เป็น Root Class</li>
<li>คลาสที่ต่อออกมาจาก animal ก็คือ mammal ( สัตว์เลี้ยงลูกด้วยนม )  ดังนั้น parent class ของ mammal ก็คือ Animal</li>
<li>Fish เป็น class ที่สืบทอดมากจา animal พูดง่ายๆว่าเป็น child ของ animal และก็แน่นอนว่า Fish มี parent class คือ animal เช่นเดียวกัน</li>
<li>และสุดท้าย mammal มี child class ที่ชื่อว่า dog และในทางกลับกัน dog ก็จะมี parent class เป็น Mammal</li>
</ul>
<p>ถ้าหากเราให้คุณสมบัติของ class ต่างๆ เช่นว่า </p>
<ul>
<li>animal เป็นสิ่งมีชีวิตที่ต้องกินอาหาร </li>
<li>mammal  เลี้ยงลูกด้วยนม </li>
<li>fish หายใจในน้ำได้ </li>
</ul>
<p>นั่นก็แปลได้ว่า dog นั้นเป็นสิ่งมีชีวิตที่ต้องกินอาหาร ( สืบทอดจาก animal ) และเป็นสัตว์เลี้ยงลูกด้วยนม ( สืบทอดจาก mammal ) แต่ไม่อาจจะหายใจในน้ำได้เพราะไม่ได้สืบทอดมาจาก Fish</p>
<p>เราลองมาเขียน class จากแผนภาพในแบบง่ายๆกัน</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// animal class</span>
<span style="color: #a61390;">@interface</span> Animal
<span style="color: #002200;">&#123;</span>
<span style="color: #a61390;">string</span> name;
<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> eat;</pre></div></div>

<p>จาก code ข้างบน ประกาศ class ชื่อว่า animal โดยมี ตัวแปรเป็น string เอาไว้เก็บชื่อของสัตว์ตัวนี้ และสัตว์ต่างๆก็ต้องการ การกินอาหาร เลยประกาศ method ชื่อว่า eat ขึ้นมา</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Mammal.h</span>
<span style="color: #a61390;">@interface</span> Mammal <span style="color: #002200;">:</span>Animal
<span style="color: #002200;">&#123;</span>
<span style="color: #a61390;">int</span> blood_temperature;
<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> feedMilk;</pre></div></div>

<p>แล้วก็ประกาศ class ชื่อว่า mammal  ขึ้นมา โดยสืบทอดมาจาก animal และก็เพิ่ม method ชื่อว่า feedMilk เข้าไป  จากตรงนี้ Mammal ก็จะสามารถเรียกใช้ method ของ animal ได้ด้วย นั่นก็คือ eat นั่นเอง และส่วนตัวของ mammal เองก็มี method ของตัวเองที่ชื่อ feedMilk ด้วย</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// dog.h</span>
<span style="color: #a61390;">@interface</span> Dog <span style="color: #002200;">:</span>Mammal
<span style="color: #002200;">&#123;</span>
<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> bite;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> run;</pre></div></div>

<p>สุดท้ายที่เราประกาศก็คือ dog โดยเพิ่ม method เข้ามาอีกสองอันนั่นก็คือ bite กับ run</p>
<p>สรุปได้ว่า ตอนนี้ dog นั้นสามารถ eat , feedMilk , bite , run ทำได้ทั้ง 4 อย่างเลย<br />
จะเห็นได้ว่า inheritance นั้นมีประโยชน์มาก เพราะว่าเราสามารถ สร้าง class ใหม่ขึ้นมาได้ จาก class ที่มีอยู่แล้ว ทำให้เราลดเวลาการเขียนโปรแกรมลงได้ได้เยอะมาก และเป็นส่วนสำคัญสำหรับ การเขียนโปรแกรมในแบบ OOP ( Object Oriented Programming ) </p>

<!-- start wp-tags-to-technorati 1.02 -->

<p class='technorati-tags'>Technorati Tags: <a class='technorati-link' href='http://technorati.com/tag/basic' rel='tag' target='_self'>basic</a>, <a class='technorati-link' href='http://technorati.com/tag/class' rel='tag' target='_self'>class</a>, <a class='technorati-link' href='http://technorati.com/tag/objective-c' rel='tag' target='_self'>objective-c</a>, <a class='technorati-link' href='http://technorati.com/tag/programming' rel='tag' target='_self'>programming</a></p>

<!-- end wp-tags-to-technorati -->
]]></content:encoded>
			<wfw:commentRss>http://www.macfeteria.com/blog/2008/02/24/more-about-class-inheritance/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Objective-C Programming Part II.</title>
		<link>http://www.macfeteria.com/blog/2008/02/23/objective-c-programming-part-ii/</link>
		<comments>http://www.macfeteria.com/blog/2008/02/23/objective-c-programming-part-ii/#comments</comments>
		<pubDate>Sat, 23 Feb 2008 11:43:38 +0000</pubDate>
		<dc:creator>[Ter]</dc:creator>
				<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[basic]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[objective-c]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://maccafe.wordpress.com/?p=7</guid>
		<description><![CDATA[หลังจากที่เขียน Objective-C ไปแล้วอันแรก ผมคิดว่าถ้าจะเริ่มตั้งแต่ ชนิดของตัวแปร Loop , if-else ก็คิดว่าคงจะใช้เวลาไปหน่อย กว่าจะเขียนเสร็จ ( คือถ้าจะเอาให้ละเอียดเนี่ยสงสัยว่าเขียน หนังสือเลยอาจจะดีกว่า ) เอาเป็นว่า จากนี้ผมขอคิดว่า คนที่อ่านบทความของผม นั้น มีความรู้เกี่ยวกับ C/C++ มาบ้าง ซึ่งสิ่งที่คุณต้องรู้มาก่อนก็คือ Data type ก็จำพวกว่า สามารถประกาศ int , char , array ได้ประมาณนี้ก็พอ Expression เป็นต้นว่า เข้าใจการใช้งาน if &#8211; else , neatest if-else อะไรทำนองนี้ Function ใช้งานฟังชั่นในเบื้องต้นเป็น รวมทั้งความเข้าใจในการส่ง Parameter และการส่งค่ากลับของ function Pointer เข้าใจการทำงานของ pointer และการใช้งานเป็นในระดับหนึ่ง Basic Memory Management [...]]]></description>
			<content:encoded><![CDATA[<p>หลังจากที่เขียน Objective-C ไปแล้วอันแรก ผมคิดว่าถ้าจะเริ่มตั้งแต่ ชนิดของตัวแปร Loop , if-else ก็คิดว่าคงจะใช้เวลาไปหน่อย กว่าจะเขียนเสร็จ ( คือถ้าจะเอาให้ละเอียดเนี่ยสงสัยว่าเขียน หนังสือเลยอาจจะดีกว่า ) เอาเป็นว่า จากนี้ผมขอคิดว่า คนที่อ่านบทความของผม นั้น มีความรู้เกี่ยวกับ C/C++ มาบ้าง ซึ่งสิ่งที่คุณต้องรู้มาก่อนก็คือ</p>
<ul>
<li>Data type<br />
ก็จำพวกว่า สามารถประกาศ int , char , array ได้ประมาณนี้ก็พอ</li>
<li>Expression<br />
เป็นต้นว่า เข้าใจการใช้งาน if &#8211; else , neatest if-else อะไรทำนองนี้</li>
<li>Function<br />
ใช้งานฟังชั่นในเบื้องต้นเป็น รวมทั้งความเข้าใจในการส่ง Parameter และการส่งค่ากลับของ function</li>
<li>Pointer<br />
เข้าใจการทำงานของ pointer และการใช้งานเป็นในระดับหนึ่ง</li>
<li>Basic Memory Management<br />
รู้และเข้าใจการจอง memory และการใช้งาน เป็นต้นว่า malloc , free , delete</li>
</ul>
<p>คือเพราะว่า โดยภาษา Objective-C นั้น มีการใช้งาน Syntax คล้ายๆกับ ภาษา C และ java ค่อนข้างมาก สำหรับคนที่ไม่เคยเขียน โปรแกรมมาก่อน แนะนำว่า ควรไปศึกษา C/C++ มาก่อน ( ผมว่าจะเขียน เหมือนกันแต่เอาให้ obj-c เสร็จก่อนละกัน )</p>
<h4>การเขียน Class</h4>
<p>การเขียน class ในภาษา objc นั้นจะแบ่งออกเป็น สามส่วนหลักๆคือ</p>
<ol>
<li>interface</li>
<li>implement</li>
<li>program</li>
</ol>
<p>ในภาษา objc นั้น มีรูปแบบการประกาศแบบนี้ครับ</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> classname<span style="color: #002200;">:</span> parentClassname
<span style="color: #002200;">&#123;</span>
<span style="color: #11740a; font-style: italic;">// Data member</span>
<span style="color: #002200;">&#125;</span>
function
<span style="color: #a61390;">@end</span></pre></div></div>

<p>การประกาศ class นั้นจะเริ่มด้วย @interface แล้วก็ตามด้วยชื่อของ classname และสำหรับ :parentClassname  นั้นก็เป็นส่วนที่บอกว่า class นี้สืบทอด (inheritance) มาจาก class ไหน ในส่วนของ data member นั้นจะประกาศภายในเครื่องหมาย { และ } ส่วน function จะทำการประกาศ ข้างนอก { }  และปิดท้ายด้วย @end</p>
<p>เอาละมาดูตัวอย่าง class แรกของเราเลยละกัน</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// book.h</span>
<span style="color: #a61390;">@interface</span> Book <span style="color: #002200;">:</span><span style="color: #400080;">NSObject</span>
<span style="color: #002200;">&#123;</span>
<span style="color: #a61390;">int</span> book_id;
<span style="color: #a61390;">float</span> price;
<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> printDetail;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> setPrice<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span> <span style="color: #a61390;">float</span> <span style="color: #002200;">&#41;</span> p;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> setID<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span> <span style="color: #a61390;">int</span> <span style="color: #002200;">&#41;</span> <span style="color: #a61390;">id</span>;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> setPriceAndID<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">float</span><span style="color: #002200;">&#41;</span> p<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span> <span style="color: #a61390;">id</span>;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">float</span><span style="color: #002200;">&#41;</span> getPrice;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>เราทำการสร้าง class ขึ้นมาใหม่ ชื่อว่า Book ซึ่งประกอบไปด้วย ข้อมูลสองอย่างคือ book_id และ price โดย class นี้สืบทอดมาจาก NSObject และประกอบด้วย 5 method ( function )  นั่นคือ</p>
<ul>
<li>- (void) printDetail ;<br />
สำหรับ method นี้ ได้สั่งค่ากลับเป็นแบบ void ( พูดอีกนัยได้ว่าไม่ต้องส่งค่ากลับ )</li>
<li>- (void) setPrice: (float) p;<br />
ในส่วนของ setPrice นั้นมีการส่งค่ากลับเป็นแบบ void เหมือนกัน แต่ว่าสิ่งที่ต่างจาก printDetail คือ มีการรับ parameter เข้าไปด้วย โดยรับเข้าไปเป็น int และให้ parameter นี้ชื่อว่า p</li>
<li>- (void) setID: (int) id;<br />
ทำงานเหมือนกับ setPrice แต่เป็นการใส่ค่าให้กับ book_id</li>
<li>- (void) setPriceAndID: (float) p : (int) id;<br />
method นี้จะทำการรับ ค่า parameter เข้ามาสองค่า คือ float กับ int การรับค่า parameter เข้ามาหลายๆค่านั้น จะทำการ แบ่งด้วยเครื่องหมาย <strong>: </strong>(โคล่อน )</li>
<li>- (float) getPrice;<br />
ฟังชั่นนี้จะทำการส่งค่า price กลับไปเป็นแบบ float</li>
</ul>
<p>เพียงเท่านี้ก็เป็นการประกาศ Class แบบง่ายๆเสร็จแล้ว</p>
<p>อาจจะงงว่าทำไมต้อง inherit มาจาก NSObject ด้วย ก็ขออธิบายเพิ่มเติมง่ายๆว่า มันคือ Root ของ class ต่างๆในภาษา objective-c สำหรับคำว่า NS นั้น ย่อมาจาก NextStep (เป็นชื่อของบริษัทเก่าที่ Steve Job เคยตั้ง) NSObject นั้นจะมี function ที่สำคัญอันได้แก่ การจอง memory ( alloc ) ให้กับ object นั้นๆ และการ เลิกการจอง ( dealloc) พูดง่ายว่า การประกาศ class ใหม่นั้นต้องสืบทอดมาจาก class นี้</p>
<p>เมื่อทำการประกาศ class ไปแล้ว เรายังเหลือส่วนที่ต้องเขียน นั่นก็คือ ส่วนของ implementation งั้นมาดูกันเลย</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//book.m</span>
<span style="color: #6e371a;">#import &lt;stdio.h&gt;</span>
<span style="color: #a61390;">@implementation</span> Book
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> printDetail <span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">printf</span><span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">&quot;book id: %i , price %f&quot;</span>, book_id, price <span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> setPrice<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">float</span><span style="color: #002200;">&#41;</span> p <span style="color: #002200;">&#123;</span>
	price <span style="color: #002200;">=</span> p;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> setID<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> <span style="color: #002200;">&#41;</span> <span style="color: #a61390;">id</span> <span style="color: #002200;">&#123;</span>
	book_id <span style="color: #002200;">=</span> <span style="color: #a61390;">id</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> setPriceAndID<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">float</span><span style="color: #002200;">&#41;</span> p<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span> <span style="color: #a61390;">id</span> <span style="color: #002200;">&#123;</span>
	book_id <span style="color: #002200;">=</span> <span style="color: #a61390;">id</span>;
	price <span style="color: #002200;">=</span> p;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">float</span><span style="color: #002200;">&#41;</span> getPrice<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> price;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>ก็เป็นว่าเสร็จในส่วนของการ implementation แล้ว เหลือส่วนสุดท้ายนั่นก็คือ program</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// main.m</span>
<span style="color: #6e371a;">#import &quot;book.h&quot;</span>
<span style="color: #a61390;">int</span> main<span style="color: #002200;">&#40;</span> <span style="color: #a61390;">int</span> argc, <span style="color: #a61390;">const</span> <span style="color: #a61390;">char</span> <span style="color: #002200;">*</span>argv<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">// create a new instance</span>
	Book <span style="color: #002200;">*</span>scibook;
	scibook <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>Book alloc<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>scibook init<span style="color: #002200;">&#93;</span>;
	Book <span style="color: #002200;">*</span>mathbook <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>Book alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
	<span style="color: #11740a; font-style: italic;">// set the values</span>
	<span style="color: #002200;">&#91;</span>mathbook setID<span style="color: #002200;">:</span> <span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>mathbook setPrice<span style="color: #002200;">:</span> <span style="color: #2400d9;">3</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>scibook setPriceAndID<span style="color: #002200;">:</span> <span style="color: #2400d9;">2</span> <span style="color: #002200;">:</span> <span style="color: #2400d9;">5</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// print it</span>
	<span style="color: #a61390;">printf</span><span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">&quot;The Book detail: <span style="color: #2400d9;">\n</span> &quot;</span> <span style="color: #002200;">&#41;</span>;
	<span style="color: #002200;">&#91;</span>mathbook printDetail<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>scibook printDetail<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">printf</span><span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">&quot;<span style="color: #2400d9;">\n</span>&quot;</span> <span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// free memory</span>
	<span style="color: #002200;">&#91;</span>mathbook release<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>scibook release<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">return</span> <span style="color: #2400d9;">0</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<h2>Sending Message</h2>
<p>สำหรับ ภาษา  objective-c นั้นจะทำการเรียก function หรือว่า method ว่าเป็นการส่ง message ไปให้มัน ซึ่งรูปแบบก็คือ</p>
<p><strong>[receiver message];</strong></p>
<p>ซึ่งถ้าเทียบกับภาษา C/C++ ก็จะได้ประมาณว่า</p>
<p><strong>receiver-&gt;message();</strong></p>
<p>เหมือนเราต้องการจะให้ receiver นั้นทำอะไร เราก็ต้องบอกให้มันทำให้เรานั่นเอง</p>
<p>ขออธิบายในส่วนของ main program กันก่อนละกัน</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">Book <span style="color: #002200;">*</span>scibook;
scibook <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>Book alloc<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>scibook init<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>เราเริ่มประกาศตัวแปร scibook ให้เป็น pointer ของ Book และทำการส่ง message <strong>ที่ชื่อว่า alloc</strong> ไปให้ Book เพื่อทำการจอง memory แล้วหลังจากการจองแล้ว ก็ยังไม่สามารถใช้ได้เลย ต้องทำการ initialize มันซะก่อน ด้วยการส่ง message บอกให้ <strong>init</strong> อีกที</p>
<p>ปล. initialize คือการให้ค่าเริ่มต้นสำหรับตัวแปรต่างๆ เพราะว่าในการจอง memory  นั้น พื้นที่ที่โดนจองอาจจะมีค่าต่างๆที่หลงเหลือจากโปรแกรม อื่นๆ เราก็ต้องปัดกวาด พื้นที่ตรงนั้นให้มันพร้อม เสียก่อน</p>
<p>สำหรับ บรรทัดต่อมา เป็นการรวมสองบรรทัดเข้าด้วยกันเลย ก็จะได้ว่า</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">Book <span style="color: #002200;">*</span>mathbook <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>Book alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>การทำงานเหมือน กับ scibook เลยแค่ ลดให้มันเหลือ 1 บรรทัด อาจจะมองได้ดังรูปต่อไปนี้</p>
<p><img class="aligncenter size-full wp-image-466" title="receiver" src="http://www.macfeteria.com/wp-content/uploads/2008/02/receiver.png" alt="receiver" width="150" height="80" /></p>
<p>เมื่อเรามี  object แล้วก็สามารถเรียกใช้ function โดยการส่ง message ไปให้ object เช่น</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span>mathbook setID<span style="color: #002200;">:</span> <span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>จากตัวอย่างเป็นการส่ง message ที่ชื่อว่า  setID และส่ง parameter ที่มีค่าเป็น 1 ไปให้ตัวแปรที่ชื่อ mathbook ด้วย<br />
และส่วนสุดท้าย</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span>mathbook release<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>เป็นการส่ง message ที่ชื่อ release เพื่อคืนหน่วยความจำให้กับระบบ เราต้องทำทุกครั้งเมื่อจะจบโปรแกรม เพราะว่าไม่งั้นมันจะไม่รู้ว่าโปรแกรมของเราได้ยกเลิกการใช้งานในพื้นที่ตรงนั้นไปแล้ว</p>
<p>ถ้าลอง compile และ run ก็จะได้ผลลัพธ์แบบนี้ครับ</p>
<pre>The Book detail:
book id: 1 , price 3.0000
book id: 5 , price 2.0000</pre>
<p>ก็ลองๆ โหลด source file ไป compile ดูได้ครับ</p>
<p><a href="http://www.macfeteria.com/wp-content/uploads/2008/07/objc_part_ii.zip">Objective-C Programming Part I Source File</a></p>

<!-- start wp-tags-to-technorati 1.02 -->

<p class='technorati-tags'>Technorati Tags: <a class='technorati-link' href='http://technorati.com/tag/basic' rel='tag' target='_self'>basic</a>, <a class='technorati-link' href='http://technorati.com/tag/class' rel='tag' target='_self'>class</a>, <a class='technorati-link' href='http://technorati.com/tag/objective-c' rel='tag' target='_self'>objective-c</a>, <a class='technorati-link' href='http://technorati.com/tag/programming' rel='tag' target='_self'>programming</a></p>

<!-- end wp-tags-to-technorati -->
]]></content:encoded>
			<wfw:commentRss>http://www.macfeteria.com/blog/2008/02/23/objective-c-programming-part-ii/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

