oc-Object-oriented

Source: Internet
Author: User

Object-Oriented Programming OOP (Object-oriented programming)

@interface Circle:nsobject {

Shapecolor FillColor;

Shaperect bounds;

}

-(void) Setfillcolor: (Shapecolor) FillColor;

-(void) SetBounds: (shaperect) bounds;

-(void) draw;

@end//circle

The first line of code in the above code is as follows

@interface Circle:nsobject

As long as you see the @ symbol in Objective-c, you can think of it as an extension of the C language.

@interface Circle tells the editor: "This is the interface for the new class circle"

/*

The nsobject in the @interface line tells the compiler that the Circle class is based on the NSObject class, which indicates that each circle class is a nsobject, and that each circle class inherits all the behavior defined by the NSObject class.

*/

After declaring the new class, we will tell the compiler the various data members that the Circle object requires.

{

Shapecolor FillColor;

Shaperect bounds;

}

After you specify fillcolor and bounds in a class declaration, each time you create a circle object, the two elements are included in the object. Therefore, each circle class object will have its own fillcolor and bounds. The values of FillColor and bounds are called instance variables of the circle class (instance variable).

The curly brace at the end tells the compiler that we have specified an instance variable for the Circle class

-(void) draw;

-(void) Setfillcolor: (Shapecolor) FillColor;

-(void) SetBounds: (shaperect) bounds;

They are called method declarations (methods declaration),

Inside the parentheses is the return type of the method.

-(void) Setfillcolor: (Shapecolor) FillColor;

-(void) SetBounds: (shaperect) bounds;

There is a syntax technique called infix in Objective-c, where the name and parameters of the method are all together, for example, you can call a method with a parameter

[Circle Setfillcolor:kredcolor];

A method call with two parameters is as follows:

[textthing setstringvalue: @ "Hello there" color:kbluecolor];

SetStringValue and color are the names of the arguments @ "Hello there" and Kbluecolor are passed arguments.

"Void" indicates no return value, ":" indicates that a parameter will appear later

2. > Object-oriented core is classes and objects

The definition of a class is divided into two parts: the interface part and the implementation part

Interface part: Characteristics and behavior of the foreign Declaration class

Implementation part: Concrete implementation of behavior

A class is an abstract concept in which the operation of a program is accomplished through mutual collaboration between objects.

Object is an instance of a class that creates an object from a class

Creating an object is divided into two parts:

Allocates memory space .> allocates memory to the heap, and returns the first address based on the instance variables declared in the class

Initialize .> to set an initial value for an instance variable of an object

Person *person = [[Person alloc] init];

The above code contains the creation and initialization of objects.

+ (Instancetype) alloc;+ represents a class method that can only be called by a class, Instancetype returns a value type and can be replaced with an ID class

-(instancetype) init; -Represents the object method, object invocation, Instancetype return value type

The difference between Instanetype and ID:

Instancetype can return objects of the same type as the method's class, and the ID can only return objects of unknown type

The instancetype can be used only as a return value and as a parameter, and the ID also defines the variable

Instancetype will tell the compiler the current type, but the ID is untyped for the compiler, and calling any method will not give an error.

oc-Object-oriented

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.