คลาส อ็อบเจ็กต์ และเมธอด
เรียนรู้การกำหนดคลาส สร้างอ็อบเจ็กต์ และนำเมธอดของอินสแตนซ์และคลาสมาใช้เพื่อห่อหุ้มพฤติกรรม
คลาส อ็อบเจ็กต์ และเมธอด เป็นบทเรียน Objective-C iOS Development for Legacy & Enterprise Apps ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Objective-C iOS Development for Legacy & Enterprise Apps และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Objective-C iOS Development for Legacy & Enterprise Apps มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
OOP Basics: Classes & Objects
Welcome to Object-Oriented Programming (OOP) in Objective-C! OOP helps organize code into reusable blueprints called classes.
From these blueprints, we create individual items called objects. Think of a class as a cookie cutter, and an object as a cookie!
Your First Class: Header File
Every Objective-C class has two main files: a .h (header) and a .m (implementation) file.
The header file (.h) declares what your class is and what it can do. It's like the public blueprint.
Here's how to declare a simple Person class:
// Person.h
#import <Foundation/Foundation.h>
@interface Person : NSObject
// Method declaration
- (void)sayHello;
@endClass Implementation: Source File
The implementation file (.m) contains the actual code that makes your class work. It defines the behavior declared in the header.
Here, we implement the sayHello method for our Person class:
// Person.m
#import "Person.h"
@implementation Person
- (void)sayHello {
NSLog(@"Hello from a Person object!");
}
@endBringing Classes to Life: Objects
Now that we have a Person class, let's create an object from it! Creating an object is also called instantiation.
In Objective-C, we use the alloc and init methods to create new objects. alloc reserves memory, and init initializes it.
Try running this example:
#import <Foundation/Foundation.h>
// Person.h (declared here for runnable example)
@interface Person : NSObject
- (void)sayHello;
@end
// Person.m (implemented here for runnable example)
@implementation Person
- (void)sayHello {
NSLog(@"Hello from a Person object!");
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
// Create a Person object
Person *person1 = [[Person alloc] init];
NSLog(@"Created a Person object: %@", person1);
}
return 0;
}Instance Methods: Object Actions
Instance methods are actions that an individual object can perform. They usually work with data specific to that object.
To tell an object to perform an action, you send it a message. The syntax is [object methodName];.
Let's make our person1 object say hello:
#import <Foundation/Foundation.h>
@interface Person : NSObject
- (void)sayHello;
@end
@implementation Person
- (void)sayHello {
NSLog(@"Hello from a Person object!");
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
Person *person1 = [[Person alloc] init];
[person1 sayHello]; // Sending the 'sayHello' message
}
return 0;
}Methods with Arguments
Methods can also take arguments (or parameters) to customize their behavior. Objective-C uses a unique syntax for method arguments, making methods very descriptive.
The first argument is part of the method name, and subsequent arguments are labeled.
Let's add a method to our Person class that takes a name:
#import <Foundation/Foundation.h>
@interface Person : NSObject
- (void)sayHelloTo:(NSString *)name; // Method with one argument
@end
@implementation Person
- (void)sayHelloTo:(NSString *)name {
NSLog(@"Hello, %@ from a Person object!", name);
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
Person *person1 = [[Person alloc] init];
[person1 sayHelloTo:@"Alice"]; // Sending message with argument
}
return 0;
}Class Methods: Type Actions
Unlike instance methods, class methods (also known as static methods) belong to the class itself, not to a specific object.
They start with a + sign in their declaration and implementation. You call them directly on the class name, like [ClassName methodName];.
Class methods are often used for creating objects or utility functions related to the class.
#import <Foundation/Foundation.h>
@interface Person : NSObject
+ (void)aboutThisClass; // Class method declaration
@end
@implementation Person
+ (void)aboutThisClass {
NSLog(@"This is the Person class, used to create person objects.");
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
[Person aboutThisClass]; // Calling a class method
}
return 0;
}Class vs. Instance: Key Difference
Here's a quick summary of the main differences:
- Instance Methods (
-): Operate on a specific object's data. You need an object instance to call them. - Class Methods (
+): Operate on the class itself, not an instance. You call them directly on the class name. They don't have access to instance variables.
Choose the right type based on whether the action needs specific object data or general class-related functionality.
Sending Messages in Objective-C
In Objective-C, calling a method is formally called sending a message to an object (or class).
The syntax [receiver messageName:argument]; means we're sending the messageName: message to the receiver object (or class).
This message-sending mechanism is a core part of Objective-C's dynamic nature.
Quick Check: Classes & Methods
Test your knowledge of Objective-C OOP fundamentals.
Recap: Classes, Objects & Methods
Great job! In this lesson, you learned:
- What classes are (blueprints) and objects are (instances).
- How to declare a class in a
.hfile and implement it in a.mfile. - How to create objects using
allocandinit. - The difference between instance methods (
-) and class methods (+). - How Objective-C uses message sending to invoke methods.
You're now ready to build more complex, organized Objective-C applications!
คำถามที่พบบ่อย
บทเรียน “คลาส อ็อบเจ็กต์ และเมธอด” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “คลาส อ็อบเจ็กต์ และเมธอด” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Objective-C iOS Development for Legacy & Enterprise Apps ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Objective-C iOS Development for Legacy & Enterprise Apps มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “คลาส อ็อบเจ็กต์ และเมธอด”
เรียนรู้การกำหนดคลาส สร้างอ็อบเจ็กต์ และนำเมธอดของอินสแตนซ์และคลาสมาใช้เพื่อห่อหุ้มพฤติกรรม คุณปฏิบัติ 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 ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “คลาส อ็อบเจ็กต์ และเมธอด” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Objective-C iOS Development for Legacy & Enterprise Apps นี้ได้ไหม
ได้ บทเรียน Objective-C iOS Development for Legacy & Enterprise Apps ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- คลาส อ็อบเจ็กต์ และเมธอด
- พร็อพเพอร์ตีและตัวแปรของอินสแตนซ์
- โพรโทคอลและหมวดหมู่
- การสืบทอดและการเขียนทับเมธอด