Objective-c Learning Journey The third Chapter OBJECTIVE-C class declaration, definition, instance, initialization
- class declaration
// The compile-processing instructions for the class declaration begin with @interface, ending with @end, where the code is the definition of the class variable and the declaration of the method. The Declaration and definition of a class is more complex than other languages, which makes it confusing // accurate to say that other languages such as Java simply define classes without declaring them, objective-c. The objective-c needs to be declared and redefined first. @interface class Name: Parent class Name { Declare member variable ... } Declaring methods @end
Instance:
@interfaceTestcoop:nsobject {//declaring member variables intImonth; intiyear; intIday;}//declaring Methods- (void) Setyear: (int) iyear;- (void) Primalsetdata: (int) Iyear:(int) Imonth:(int) Iday;- (void) SetData: (int) Year Imonth: (int) Imonth Iday: (int) Iday;- (void) Displaydateinfo;@end/*Description: The preceding short Line/-Indicates that this is the declaration of the Objective-c method, which distinguishes between function prototypes (in C) and (Objective-c) method declarations. The short line is followed by the return type of the method/For example (void), in parentheses. The symbol "+" means that the method is the Objective-c class method notation "-" means that the method is a Objecitve-c instance method.*/
- Class implementation
// objective-c the Declaration and definition of a class completely apart, in the declaration of a class, you can only define instance variables and method names and types, // the specific implementation is used in the @implementation compiler processing instructions. @implementation class name instance method definition ... @end
Instance:
@implementationTestcoop- (void) displaydateinfo{NSLog (@"Today is:%d.%d.%d\n", Iyear, Imonth, iday);}//Note: The method implementation part of the class cannot be the same as the declaration part, but it cannot have the same name as the class variable, otherwise it hides the initial variable. //For example, the following parameter "year" cannot be the same as the "Iyear" in the Declaration- (void) Setyear: (int) year{iyear=Year ;}- (void) Primalsetdata: (int) Year:(int) Month:(int) day{iyear=Year ; Imonth=month; Iday=Day ; }- (void) SetData: (int) Year Imonth: (int) Month Iday: (int) day{iyear=Year ; Imonth=month; Iday=Day ;}
- Instantiating an Object
intMain (intargcConst Char*argv[]) {@autoreleasepool {//Insert code here ...//NSLog (@ "%d-,%s%@\n", "Hel123lo", @ "123");Testcoop *ptest = [TestcoopNew]; [Ptest Primalsetdata: the:Geneva: to]; [Ptest Displaydateinfo]; [Ptest SetData: .Imonth: .Iday: .]; [Ptest Displaydateinfo]; [Ptest setyear:1987]; [Ptest Displaydateinfo]; } return 0;}
- Initialization of the class
// cocoa is accustomed to using alloc and init to create objects instead of new // using Alloc to allocate memory and initialize with INIT, this memory will be fully cleared 0,bool type is no,int type 0, pointer is nil // the above-created object code is changed to the following: *ptest = [[Testcoop alloc] init];
Objective-c Study Tour Third article