OC and OC

Source: Internet
Author: User

OC and OC

Summary:
In oc, We need to compile a class. First, we need a. h header file and a. m implementation file. Generally, the classes we create inherit the root class, because the root class helps us implement a lot of practical methods, and the class contains variables (attributes), functions (methods ), like other languages, methods in oc are also divided into static methods (class methods) and instance methods, "+" indicates class methods, and "-" indicates instance methods.

The. h header file is a declaration and has no actual implementation content. The specific implementation should be written in the. m implementation file. The class method cannot use instance variables, but can be called directly by class. The instance method can directly use instance variables and assign values, but cannot be called directly by class. It must be instantiated, in general, Initialization is required at the same time of Instantiation.

To operate on instance variables, you must use the set and get methods. The name of the set method must start with set + The Name Of The instance variable (the first letter must be capitalized). The parameter uses the instance variable name and the data types are the same, generally, the instance variables are distinguished by an underscore (_). The set method does not return values. The get method uses the instance variable name, which does not have an underscore (_). There is no parameter, but there is a return value, the data type should be consistent with the instance variable. Of course, public instance variables can be accessed directly, but data security should be taken into account.


Note: after the set and get methods are written, you can call them in brackets or by point. You can also use the @ property attribute and @ synthesize to simplify the accessors of instance variables.
Common object initialization methods include the following: 1. Because the root class is inherited, subclass does not need to be in. the statement can be directly stated in the H file. in the m file, override the parent class to initialize the init method. 2. directly declare the method initWithName in. h with the form parameter, implement it in the. m file, and assign a value when creating the object. In this way, the initialization method with different parameters can be derived, so that you can use any Initialization Method in the future.
 

Create object initialization:
 

Here test. rStr calls the method that returns the name.

 

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.