Objective-c Introductory Tutorial (excerpt)

Source: Internet
Author: User

  •   1  #import <Foundation/Foundation.h>   3    4   5  Span style= "color: #000000;" > Intlegscount;   6   7     8   9 -(void   10  11 -( void ) Setlegscount: (int  ) count;  12  13  @end 

     

  • In objective-c, the definition of a class begins at the beginning of the @interface to the end of @end, that is, the compiler sees the @interface and knows that this is the beginning of the definition of the class, and seeing @end knows that the definition of the class is over.
  • About ": NSObject" We can now understand that, by writing this, we have free access to Apple's well-prepared series of classes and objects that are essential to our approach. NSObject is called Rootclass, which is the root class.
  • Now let's look at line 5th. We used to call this thing a variable, and from now on, we need to use Objective-c's language precisely, which is the entity variable (instancevariables, which is abbreviated as Ivars in some English materials).
  • The first letter of Line 9th is a minus sign "-". This minus sign tells the compiler that the method behind the minus sign is the entity method (Instancemethod). An entity method means that this method cannot be run until the class is not manifested. What we see here is a minus sign, and a plus sign with a minus sign, we call the method with the plus sign the class method (Classmethod), and the entity method, and the class methods can run out of the entity.

Well, here's a summary of how classes are defined as follows:
@interface class Name: The name of the parent class {entity variable type entity variable name;

...
}
-(return value type) method name;
+ (return value type) method name;
-(return value type) method Name: (variable type) variable name tag 1: (Variable type) variable 1 name;

...

@end

  •    cattle.h   " -(void  ) Saysomething { nslog ( @ " Span style= "color: #800000;" >hello,iamacattle,ihave%dlegs.   " ,legscount);  }  -(void ) Setlegscount: (int ) Count 10  { legscount  =count; }  @end    

     

  • Let's look at lines 4th and 13th, like @ in the header file, and the definition of our class here is also compiler-oriented. The compiler will consider the part from @implementation to @end as the definition of the class. There is a space behind the @implementation, and the space is followed by the name of our class cattle, which is telling the compiler that we are going to define the cattle class. Between lines 4th and 13th is the definition part of the entity method or class method that we define in the header file, and of course if our class does not have any entity methods and class methods, we may want to write @implementation and @end, leaving the middle empty.

    Line 5th is the implementation of the saysomething we define, and we can see that the 5th line is consistent with the 7th line of the header file Cattle.h. I personally believe that in writing the entity method and the definition of the class method, in order to avoid the manual input error, you can copy the part of the declaration from the beginning of the file, and then delete the semicolon, plus two curly braces. We know that line 6 to line 8th is the definition part of the method, and we'll look at line 7th. The Hello,world output in line 7th and chapter Two is similar, except for one%d, and the entity variable Legscount, which is similar to printf in C, which uses legscount instead of%d in the string.
    The 9th line is the same as the 8th line of Cattle.h, which does not need to be explained. Let's take a look at line 11th, and line 11th is saying that the value of the parameter count is assigned to the entity variable Legscount. We can control the value of Legscount inside the cattle object by using the Setlegscount method.
    This part of the key points for @implementation and @end, understand this thing, the rest is not difficult to understand. Let's summarize the syntax of the definition section of the class:

    @implementation Class Name-(method return value) method name {
    Method definition}
    -(Method return value) method name: (variable type) variable name {
    Method definition} @end

  • 1 #import<Foundation/Foundation.h> 2 3 #import"Cattle.h" 4 5Intmain (intargc,constchar*argv[])6 {7  8Nsautoreleasepool*pool=[[Nsautoreleasepoolalloc]init];9 Tenidcattle=[cattlenew]; One  A[Cattlesetlegscount:4];  -  - [cattlesaysomething]; the  - [Pooldrain]; -  - Return0; +  -}

    Students see the first word ID in line 10th. ID is the abbreviation of English identifier, we have encountered in many places ID, for example, in the blog park, we use the ID to login system, our ID represents a user of the system. Because the ID is unique within a system, the system obtains our ID and knows who we are. Objective-c is the same thing, using an ID to represent an object, in Objective-c, all objects can be distinguished using IDs. We know that a class is just some data plus code that operates on that data, so the ID is actually a pointer to a data structure, equivalent to void*.  
    The second word in line 10th is cattle, which is the name we give to this ID. Of course, you can make any name other than the system's reserved name, but in order to maintain the readability of the code, we need a meaningful name, where we use the lowercase cattle of the header text.  
    Line 10th of [cattlenew] is the creation of objects, new is actually a combination of alloc and INIT, the creation of objects in Objective-c is a process of allocating memory and initialization for objects. New,alloc and init are defined in cattle's superclass nsobject, the author will explain in detail how to create objects in the 7th chapter. We used new to create the object before the 7th chapter.

  • The use of methods inside Objective-c is somewhat different from other languages, and objective-c uses messages to invoke methods. So the author thinks that before explaining the part of the 7th line equals to the right, we need to first introduce a new friend, message. The so-called message is a class or an action that an object can perform. The format of the message is as follows:
    [Object or class name Method name: parameter sequence]; First we observe that there are two brackets, and the rightmost parenthesis is followed by a semicolon, and when the compiler encounters this format, it sends the middle part as a message. In the expression above, including the contents of all parts of the brackets is called the message expression (messageexpression), "Object or class name" is called the receiver (receiver), that is, the recipient of the message, "method name: Parameter sequence" is called a message ( Message), the "method name" is called the selector (Selector) or the keyword (Keyword). Objective-c and C are fully compatible, and the C language brackets are used to represent arrays, but the format of the arrays is not the same as the format of the messages sent, so we can rest assured that the compiler will not send our message as an array.
    Let's recall the process of invoking the function in C, in fact, the compiler has already set the function relative to the entire execution package's entry address, and the execution of the function is actually executed directly from this address. Objective-c uses an indirect way of sending messages to an object or class (specifically, whether the name of the object or class depends on whether the method is an entity method or a class method), and the message should be in the same format as the method. Specifically, the section on the right side of the 7th line equals [cattlenew] means sending a new message to the cattle class. So when the cattle class receives new, it looks for a list of the corresponding messages, finds the new class method after it has been found, allocates the memory and returns an ID after initialization is complete, so we get an object.

subclass Subclass and Super-class superclass

  • @interfaceCattle: nsobject{

    This code is telling the compiler that our cattle is an inherited nsobject. In this code, NSObject is a superclass, and cattle is a subclass. By writing this, we used to get a free nsobject inside a method called New: ()

  • Idcattle=[cattlenew];

  • In object-oriented program design, if you inherit superclass from subclasses, some of the code in the superclass is still valid in the subclass, which greatly improves the efficiency of the Code. Based on the superclass we can put some of the functions we need to append to the subclass, in this chapter we decided to regenerate a subclass bull based on the cattle class:
    1 #import<Foundation/Foundation.h> 2 #import"Cattle.h"  3 @interfaceBull: Cattle4 { 5nsstring*Skincolor;6 } 7-(void) saysomething; 8-(nsstring*) Getskincolor; 9-(void) Setskincolor: (nsstring*) color; Ten @end

    The 2nd line in the previous section of the code is to inform the compiler that the declaration part of our class needs to Cattle.h the file. We are already familiar with this document, which we have built in the 3rd chapter, and in this chapter we will not change any of the contents.
    Line 4th, is to inform the compiler, we need to declare a class name called Bull, inherit from the cattle inside. In line 5th, we append an instance variable, Skincolor, to save the bull color.
    In line 7th, we reload the existing (void) SaySomething instance method in the cattle class. The main reason for the overloaded (void) SaySomething method is that we think that bull should be different from cattle. Lines 8th through 9th, we declare two new methods (nsstring*) for the Bull class Getskincolor and
    (void) Setskincolor: (nsstring*) color, which is used to set and read our instance variable Skincolor, respectively. OK, let's summarize the format of the subclass when inheriting.

    @interface class Name: The name of the parent class {entity variable type entity variable name;} -(return value type) overloaded method name; +(return value type) overloaded method name;  -(return value type) Other method name: (variable type) variable name: (variable type) variable name;  @end

Objective-c Introductory Tutorial (excerpt)

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.