Thứ Năm, 27 tháng 6, 2013

LẬP TRÌNH OBJECTIVE-C - part 3

4.  Class, objects, method cơ bản

a. Class:


-          File .h (Header File) là file quản lý các khai báo liên quan, cung cấp cho chúng ta cái nhìn về mặt tổng quan của code. Về lớp, loại, chức năng, khai báo hằng số, thuộc tính
-           File .m (Source File ) là file mở rộng được thừa kế từ header file, chứa nội dung chính bao gồm code ojective-C
* File .h (Header File)
@interface SimpleClass : NSObject
@end

-          Tại lớp này chúng ta khai báo lớp có tên là SimpleClass, được kế thừa NSObject.
@interface Person : NSObject
@property NSString *firstName;
@property NSString *lastName;
@end

-          Đối tượng Person có 2 thuộc tính : firstName, lastName đều là biến con trỏ (*) thuộc kiểu chuỗi NSString, kết thúc dòng code bằng dấu (;). Lớp Person thêm 1 thuộc tính mới yearOfBirth là chữ số
@end
@property NSNumber *yearOfBirth;
//Or
@property int yearOfBirth;

* File .m (Source File )
-          Chúng ta có file XYZPerson.h
@interface XYZPerson : NSObject
@property (readonly) NSString *firstName;
@property (readonly) NSString *lastName;
- (void)sayHello;
@end

-          Class kế thừa class “XYZPerson.h”
#import "XYZPerson.h"
@implementation XYZPerson
- (void)sayHello {
NSLog(@"Hello, World!");
}
@end

b. Method
Hàm cơ bản
-          Hàm trong C:
void SomeFunction();
-          Hàm trong Objective-C
- (void)someMethod;

- (void)someMethod {
NSString *myString = // get a string from somewhere...
}

Hàm có tham số truyền vào
-          Hàm trong C:
void SomeFunction(SomeType value);

-          Hàm trong Objective-C
- (void)someMethodWithValue:(SomeType)value;

- (void)someMethodWithValue:( NSString *)value {
NSLog(@"%@",value);
}

-          Tương tự với nhiều tham số
- (void)someMethodWithFirstValue:(SomeType)value1 secondValue:(AnotherType)value2;

Hàm số trả về
-          Trong C
int magicNumber {
return 42;
}

-          Trong Objective-C
- (int) magicNumber {
return 42;
}

-          Hàm gọi nhau trong cùng class
@implementation XYZPerson
- (void)sayHello {
[self saySomething:@"Hello, world!"];
}
- (void)saySomething:(NSString *)greeting {
NSLog(@"%@", greeting);
}
@end

c. Object
-          Khởi tạo object mới
NSString* myString = [NSString string];
NSString* myString = [[NSString alloc] init];
NSNumber* value = [[NSNumber alloc] initWithFloat:1.0];
// string1 will be released automatically
NSString* string1 = [NSString string];
// must release this when done
NSString *someString = @"Hello, World!";
NSString *someString = [NSString stringWithCString:"Hello, World!"
encoding:NSUTF8StringEncoding];
NSString* string2 = [[NSString alloc] init];
[string2 release];

-        Tạo đối tượng động:
-       hàm này trả về là id, đây là từ khóa đặc biết trong Objective-C (http://developer.apple.com định nghĩa là “some kind of object”). Theo tôi hiểu rằng đây là 1 dạng đối tượng đặc biệt giống với NSObject * nhưng nó không có dấu *
+ (id)alloc;
//or
- (id)init;

-          alloc (xem phần 7 – quản lý bộ nhớ) : nó gọi tới hàm init
NSObject *newObject = [[NSObject alloc] init];
tài liệu tham khảo

Không có nhận xét nào:

Đăng nhận xét