Zhang OC Basics Review 01_ class creation, declaration attributes, and nature

Source: Internet
Author: User

1. How to write a class declaration

Start with @interface , end with @end , and then write the name of the object in the same place as the class name.

Note: The first character of the class name must be capitalized

The purpose of declaring a class is to tell the system what properties and behaviors we have in this class

The properties in the OC class declaration can only be in {} between write @interface and @end

Note: When writing OC class properties, it is recommended to precede all the names of the properties with "_", which is the specification, just remember.

After the class name: NSObject is to allow our iphone class to have the ability to create objects, that is, you can directly use [Iphone new], to create a new object, or there is no such new method.

@interfaceiphone:nsobject{//Note: By default, the properties in the OC object are not directly accessible    @public  //as long as the properties in the class are exposed, you can then manipulate the properties in the object directly through a pointer to the struct body    float_model;//Model 0    int_CPU;//Cup 0    Double_size;//size 0    int_color;//Color 0}//method----@end

2. How to write a class implementation

Start with a @implementation , end with a @end , and then write the name of the class declared when the class corresponds, and must be exactly the same as the declared class name.

@implementation Iphone // the implementation of the behavior, can object methods, can class methods @end

How to create an object from a class

In OC you want to create an object from a class, you must send a message to the class

How do I send a message? In OC, if you want to send a message, write the [class name/object name Method name] first.

Whenever a new method of a class is called through a class, that is, when a message called New is sent to the class

3 things will be done inside the system.

1. Create an object for iphone class to allocate storage space

2. Initialize the properties in the object created by the iphone class

3. Returns the address of the object created by the iphone class

received the address of the iphone object via an iphone type pointer

If you use a pointer to save an object's address, then we call this pointer a type of object (understand this sentence)

Using the iphone type pointer to save the address of the iphone object, we'll call the iphone type pointer p as the iphone object

   New ];    P3.5;//But this is directly accessible, unlike C, where the properties of a class are protected.    P 0 ;    P4;    P1;

The class in OC is essentially a struct, so p is actually pointing to a struct

 structPerson {intAge ; Char*name;    }; structPerson sp; structPerson *sip = &sp; (*SIP). Age = -;//Change the value in the struct body that the pointer only wants (*SIP). Name ="LNJ"; SIP->age = -; SIP->name ="LNJ";

Zhang OC Basics Review 01_ class creation, declaration attributes, and nature

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.