การสืบทอดและการเขียนทับเมธอด
สร้างลำดับชั้นคลาสใน Objective-C ด้วยการสร้างคลาสย่อย เขียนทับเมธอดที่สืบทอดมา และเรียกการทำงานของคลาสแม่ผ่าน super
การสืบทอดและการเขียนทับเมธอด เป็นบทเรียน Objective-C iOS Development for Legacy & Enterprise Apps ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Objective-C iOS Development for Legacy & Enterprise Apps และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Objective-C iOS Development for Legacy & Enterprise Apps มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
What is Inheritance?
Inheritance lets a class reuse and extend another class. The new subclass gets all the methods and properties of its superclass, then adds or changes behavior.
Declaring a Subclass
The colon syntax in the interface declares the superclass. Here Dog inherits from Animal.
@interface Dog : Animal
- (void)fetch;
@endInherited Members
A subclass automatically has the superclass's public methods and properties — no need to redeclare them.
Dog *d = [[Dog alloc] init];
[d eat]; // inherited from Animal
[d fetch]; // defined on DogOverriding a Method
To change inherited behavior, redeclare a method with the same name in the subclass. This is overriding.
@implementation Dog
- (void)makeSound {
NSLog(@"Woof!");
}
@endCalling super
Use super to invoke the superclass version, extending rather than replacing its behavior.
- (void)makeSound {
[super makeSound];
NSLog(@"...and wags tail");
}Overriding init
Custom initializers almost always call [super init] first to set up inherited state, then add their own.
- (instancetype)init {
self = [super init];
if (self) {
_legs = 4;
}
return self;
}Polymorphism
A superclass-typed variable can hold any subclass instance. The correct overridden method runs at runtime — this is polymorphism.
Animal *pet = [[Dog alloc] init];
[pet makeSound]; // prints Woof!The instancetype Return
Initializers return instancetype, which adapts to the actual class being created — safer than hardcoding the type.
+ (instancetype)dogWithName:(NSString *)name;Checking Class at Runtime
Objective-C is dynamic. You can ask an object about its class.
if ([pet isKindOfClass:[Dog class]]) {
NSLog(@"It is a dog");
}NSObject the Root
Almost every class ultimately inherits from NSObject, which provides core behavior like alloc, init, isEqual:, and memory management hooks.
Designing Hierarchies
Favor shallow hierarchies. Use inheritance for genuine is-a relationships; for shared behavior across unrelated classes, prefer protocols and categories instead.
Quick Check
Test your inheritance knowledge.
Recap
You learned inheritance in Objective-C:
- Subclass with the
: Superclasssyntax - Inherited methods and properties come for free
- Override methods to change behavior
- Use
superto extend the parent version - Polymorphism picks the right method at runtime
Inheritance and polymorphism are pillars of OOP in Objective-C.
คำถามที่พบบ่อย
บทเรียน “การสืบทอดและการเขียนทับเมธอด” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การสืบทอดและการเขียนทับเมธอด” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Objective-C iOS Development for Legacy & Enterprise Apps ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Objective-C iOS Development for Legacy & Enterprise Apps มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การสืบทอดและการเขียนทับเมธอด”
สร้างลำดับชั้นคลาสใน Objective-C ด้วยการสร้างคลาสย่อย เขียนทับเมธอดที่สืบทอดมา และเรียกการทำงานของคลาสแม่ผ่าน super คุณปฏิบัติ Objective-C iOS Development for Legacy & Enterprise Apps ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Objective-C iOS Development for Legacy & Enterprise Apps หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Objective-C iOS Development for Legacy & Enterprise Apps บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การสืบทอดและการเขียนทับเมธอด” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Objective-C iOS Development for Legacy & Enterprise Apps นี้ได้ไหม
ได้ บทเรียน Objective-C iOS Development for Legacy & Enterprise Apps ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- คลาส อ็อบเจ็กต์ และเมธอด
- พร็อพเพอร์ตีและตัวแปรของอินสแตนซ์
- โพรโทคอลและหมวดหมู่
- การสืบทอดและการเขียนทับเมธอด