Objective-C learning notes-Part 2-Type Definition

Source: Internet
Author: User

Class Definition:

Although the compiler is not mandatory, the type of interface declaration and implementationCodeIt is best to put them in two different files for developers who use these classes to view them easily.

The implemented file ends with. M.

The interface file can use any other extension. Because it is often encoded into other files, the. h extension is usually used.

Separating interfaces and implementations is recommended in object-oriented design methods.

@ Interface classname: itssuperclass

{

Instance variable declarations

}

Method declarations

@ End

The Interface Definition starts with @ interface and ends with @ end.

The static method starts with +, and the instance method starts with-, for example:

+ Alloc;

-(Void) display;

The instance variable may have the same name as an instance method.

The return value uses the Standard C syntax for type conversion.

If the type is not specified for the method return value or parameter, the default type is used, that is, id.

If you accept an indefinite number of parameters, you can use commas and ellipsis:

-Makegroup: group ,...;

When using the relevant types, use

# Import "someclass. H" to reference A type

When defining a type interface, you need to import its parent class Interface

This chained reference ensures that all its parent classes are referenced when a class is called.

Note: For a pre-compiled package, you must reference the pre-compiled content instead of the original. h file.

If other types of member variables are used in the interface and are not mentioned in the include file, use:

@ Class retangle, circle; Method to introduce

It tells the compiler that these sybrs are type names and do not import their header files (only when the member method is not used), such:

-(Void) setprimarycolor :( nscolor *) acolor;

This aims to minimize the visible code of the compiler and connector and prevent circular references.

In short, you only need to import the interface when necessary, otherwise you only need to @ class.

If it is not a public method, you do not need to declare it in the interface.

Class implementation:

The implementation block Writing Method of the class is similar to that of the interface definition:

@ Implementation classname: itssuperclass

{

Instance variable declarations

}

Method Definitions

@ End

Each implementation file should import its own interface definition file. Some can be safely omitted: parent class name, instance variable declaration.

You do not need to add a prefix when using instance variables in the instance method. The compiler can automatically identify the variables.

In referring to the instance variable of a statically typed object, the structure pointer operator (->) Is used.

If you want to access a member variable of a typed variable, you need to use "->"

-Makeidenticaltwin

{

If (! Twin ){

Twin = [[sibling alloc] init];

Twin-> gender = gender;

Twin-> appearance = appearance;

}

Return twin;

}

Scope:

@ Private @ protected @ public @ package

The default unapplied scope is protected.

Call other instance methods or parent instance methods in the instance method:

-Reposition

{

...

[Self setorigin: somex: somey];

...

}

Or:

-Reposition

{

...

[Super setorigin: somex: somey];

...

}

Self can be assigned a value, but do not do this. Super can only be used as the message receiver.

The following code applies for an instance variable of the same type as itself in the instance method:

Self = [[rectangle alloc] init]; // bad

Id newinstance = [[rectangle alloc] init]; // good

Id newinstance = [[self alloc] init]; // excellent

The last one is the best, because it always points to the current class (so you don't have to worry about writing to the parent class in the subclass)

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.