Objective-c Study Tour Third article

Source: Internet
Author: User

Objective-c Learning Journey The third Chapter OBJECTIVE-C class declaration, definition, instance, initialization
  1. 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.*/

  2. 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 ;}

  3. 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;}

  4. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.