class= static definition เช่นคลาสของปุ่ม ความกว้างความยาวเท่าไหรสีของปุ่มอย่างไร เรียกว่า variable,method (ทำงานอย่างไร)
class = Object Type
Object= instance = object instance
data types
1) primitive data type ex. int x; //(data/type)
2) user-defined type ex. Customer *m; // (Object type, variable)
====================================
new project class object
#import <Foundation/Foundation.h> @interface Customer : NSObject { NSInteger custNumber;// variable NSString *custName;//* object type (class) } //-(void) setCustNumber : (NSInteger) n; //-(NSInteger) getCustNumber; // //-(void) setCustName : (NSString *) name; //-(NSString *) getCustName; @property(nonatomic,assign) NSInteger custNumber; @property(nonatomic,strong) NSString *custName; @end
#import "Customer.h" @implementation Customer @synthesize custNumber; @synthesize custName; @end
#import <Foundation/Foundation.h> #import "Customer.h" int main(int argc, const char * argv[]) { @autoreleasepool { // insert code here... // NSLog(@"Hello, World!"); Customer *c1=[[Customer alloc] init]; Customer *c2=[[Customer alloc] init]; [c1 setCustNumber:10]; [c1 setCustName:@"Pimon"]; [c2 setCustNumber:20]; [c2 setCustName:@"Direx"]; NSString *w=[c1 custName]; NSLog(@"Customer Name ; %@",w);//get value form w } return 0; }
เวลาใช้งาน เกิด setter getter
[obj setCustName:@”Tim”];obj.custName=@”Tim”;
getter
w=[obj.custName];
w=obj.custName;