File description:
. H class declaration file, the user declares variables, functions (methods)
. M class implementation file, the user implements the function (method) in. h)
Class declaration using keywords @ interface, @ end
Class implementation using keywords @ implementation, @ end
Code:
------------------------------------
Project file:
(OS X-> command line tool)
Main. m
Student. h
Studen. m
1 // 2 // main. m 3 // OC class learning 4 // 5 // created by Loong on 14-10-25. 6 // copyright (c) 2014 Loong. all rights reserved. 7 // 8 9 # import <Foundation/Foundation. h> 10 11 # import "student. H "12 13 int main (INT argc, const char * argv []) {14 @ autoreleasepool {15 16 // create an object 17 // [STUDENT alloc] Call the alloc static function in the Student Class 18 // [STUDENT alloc] init] in the returned type call the init function 19 student * STD = [[STUDENT alloc] init] again; 20 [STD setage: 24]; 21 int age = [STD age]; 22 23 nslog (@ "Age is: % I", age); 24 25 [STD setno: 17: 1]; 26 27 nslog (@ "Age is: % I, no is: % I", [STD age], [STD no]); 28 // [STD release]; 29} 30 return 0; 31}
1 // 2 // student. H 3 // OC class learning 4 // 5 // created by Loong on 14-10-25. 6 // copyright (c) 2014 Loong. all rights reserved. 7 // 8 9 // import header file 10 # import <Foundation/Foundation. h> 11 12 //. the H file is only used to declare those variables and functions 13 // @ interface declare a class 14 @ interface student: nsobject {// The member variable should be declared in the following braces {} 15 int _ age; // The member variable should start with _ 16 int _ No; 17} 18 19 // age get Method 20 //-represents the dynamic method, + represents the static method (static) 21-(INT) age; 22-(void) setage :( INT) age; 23 24 // set two parameters at the same time: 25-(INT) No; 26-(void) setno :( INT) Age: (INT) No; 27 28 @ end
1 // 2 // student. m 3 // OC class learning 4 // 5 // created by Loong on 14-10-25. 6 // copyright (c) 2014 Loong. all rights reserved. 7 // 8 9 # import "student. H "10 11 @ implementation student12 13-(INT) Age {14 return age; 15} 16 17-(void) setage :( INT) newage {18 age = newage; 19} 20 21 // =============== 22 23-(INT) No {24 return no; 25} 26-(void) setno :( INT) newage :( INT) newno {27 age = newage; 28 No = newno; 29} 30 @ end
// PS: Description of. In oC (description)
Student. Age is equivalent to [STUDENT getage]
Student. Age = 5; equivalent to [STUDENT setage: 5]
Objective-c Study Notes (1)