"Dark Horse programmer" OC Basic grammar--class and object related concepts

Source: Internet
Author: User

------Java Training, Android training, iOS training,. NET training, look forward to communicating with you! -------

The object-oriented part of the recent learning OC, may be because the previous learning Java and PHP accustomed to the mainstream of object-oriented thinking, access to OC object-oriented There are many statements are not accustomed to, check a lot of things, finally basically made clear, here a summary.

I. Classes and objects

1. Class: A class is an abstract concept that represents a class of things, such as: people, animals, vehicles, etc. this is no different from the concept of Java or PHP classes.

The method is defined as follows:

@interface Student:nsobject


NSString *stuname;

int stuage;

int stuid;


2. Object: OC is also known as an instance, both for the same concept is specific to the existence of a certain class, such as, a person, a car; this is a good understanding.

It is implemented as follows:

@implementation Student

Second, class methods and Object methods

1. Class method: Also known as the Plus method, which belongs to the class-level method, is shared for all objects of the class, so it can be called either with the class name or with an object instance.

Such as:

+ (int) Classacount;

2. Object method: Also known as the Minus method, belongs to an instantiated object, so it can only be called with an object instance.

Such as:

-(void) Setstuname: (NSString *) tmpstuname;

-(NSString *) stuname;

Third, member variables and attributes

The combination of the two is a better understanding.

Like the code below, what's the difference between brand and name?

@interface Car:nsobject


NSString *brand;


@property (nonatomic,copy) NSString *name;

In fact, Brand is a private member variable, and other classes do not have access to this member variable, which is obviously not conducive to encapsulation of classes.

While name is a property, it is visible to all classes throughout the project, and other classes can be accessed to this member variable.

In addition, before learning attributes, to implement the operation of member variables is generally used setter and getter method, but this is more cumbersome;

After learning the properties, it is possible to know that it is an alternative to the member variable's Read method getter and the setter of the set method, and also defines the member variable.

In the case of class-in encapsulation, the member variable is not normally accessed directly, and is accessed through properties.

Therefore, a property is not equivalent to a member variable, it is more useful than a member variable, and is easily confused by beginners. Iv. creation and use of objects in general, the creation of objects in OC is divided into three steps: objects in 1,oc are declared by pointers. If you create an object in fraction *myfraction;2,oc, use Alloc to create an object so that the compiler assigns an available memory address to the object: Myfraction=[fraction alloc];3, then initializes the object, That is, Init is called so that the object can be used: myfraction=[myfraction init];

The reason I said this alone is because I am confused by the beginning of this place.

As you learn Java, Java creates an object that is directly new, and then uses the constructor to accomplish the assignment and initialization of objects in one operation. Instead, Objective-c splits the two operations into two steps: allocation and initialization.

Assignment (allocation): The result of sending a ALLOC message to a class is to allocate a large enough memory for that class to hold all the instance variables of the class.

At the same time, the Alloc method also initializes the memory area to 0. Initialization (initialization) Gets a piece of memory from the operating system and prepares it for storage objects. The Init method almost always returns the object they are initializing. The Alloc and Init methods should be called nested as follows:

Car *car = [[Car alloc] init];

And not like this:

Car *car = [Car alloc];

[Car init];

It was later realized that this nested invocation technique was important because the initialization method returned an object that might be different from the assigned object.

"Dark Horse programmer" OC Basic grammar--class and object related concepts

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.