OC Object-oriented first OC class

Source: Internet
Author: User

After learning Java, C + + language, and then learn OC language, although also is the object-oriented language, but OC language still gives us a very "weird" feeling. This is mainly because the OC language itself has its own unique naming and use methods. Summing up the use of Java syntax and OC syntax, we will find that there are still many rules to follow. Some of the features of the OC class are summarized below.


First, about OC class

1.oc typically uses two files to describe a class that is different from Java.

2. The suffix. h file is a declaration file for a class that declares member variables, methods. Use keywords @interface and @end; Moreover, the. h file is declared only and does not implement the method of the class;

3. The suffix. m file is the implementation file for the class that implements the method declared in. h. Use keywords @implementation and @end;


Second, OC in the method

1. The Declaration and implementation of the method must begin with "+" and "-";

A plus sign represents a class method, a static method, and a minus sign represents an object method, that is, a dynamic method

2. The method declared in suffix. h is of type public and cannot be changed

           method section (+/-) + return value type + method name :+ parameter type + parameter name


Third, member variables

1. The scope of the member variable consists of three

@public can be accessed globally

@protected can only be accessed within the class and within subclasses

@private can only be accessed within the class

Less scope than Java because there is no concept of a package in OC


Iv. Use of nnstring

1.%@ can be used on any object in the output format because it uses the-(nnstring*) Description Method (inherited from the NSObject class)

2.utf8string: Convert NSString object to C string, can be printed with%s

3.NSString * variable name, pointing to a constant pointer, equivalent to char *;


OC Object-oriented first OC class

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.