The first OC class and the first oc class

Source: Internet
Author: User

The first OC class and the first oc class

The class only has three relationships:

1. Name (Class Name): the first letter is in upper case. If the name is composed of multiple words, the following words follow the hump principle.

2. attributes: generally starting with an underscore

3. behavior (method): the first letter is lowercase, followed by the hump Principle

 

Class declaration:

1. Role: Declares the attributes and behaviors of a class.

2. NSObject enables the class to create objects

 

Example: define a mobile phone class

Typedef enum {

ColorWithBlack, // do not write an error here, it is a comma, not a semicolon

ColorWithWhite.

ColorWithTuHaoJin

} Color;

 

// Class declaration

@ Interface Iphone: NSObject // enable the Iphone to create objects.

{

@ Public // external call member variables that can be reconnected

Int _ model; // Mobile Phone model

Int _ cpu; Processor

Color _ color; // Color

}

-(Void) looklMyPhone; // method without any parameters and no return value

-(Char *) receiptMessage; // method with no parameter returned value

-(Void) sendMessage :( char *) message withPhoneNumber :( char *) number; // method with parameters without return values

// Note: In oc, the method name is generally written as a sentence, so that you can be knowledgeable. Therefore, describe the parameters with modifiers before the colon. This modifier is part of the method name.

@ End // you must never lose it. @ end indicates the end of the statement.

 

// Class implementation: In the class declaration, the method that is declared is implemented in the implementation.

@ Implementation Iphone

-(Void) lookMyPhone {

NSLog (@ "cell phone cpu: % I, model: % I, color: % I", _ cpu, _ model, _ color );

}

 

-(Char *) receiptMessage {

Return "good for you ";

}

 

-(Void) sendMessage :( char *) message withPhoneNumber :( char *) number {
NSLog (@ "the message you sent to % s is: % s", number, message );
}

@ End

 

# Import <Foundation/Foundation. h>

Int main (){

// When you try to do something (execute the function), you write: [class name/object method name]

 

// Create an object

Iphone * iphone1 = [Iphone new]; // new does three things here: 1. Opening up a bucket 2. initializing a member variable 3. Returning a space address

Iphone1-> _ color = colorWithTuHaoJin;

Iphone1-> _ cpu = 4;

Iphone1-> _ model = 5;

 

// Call Method

// Method called by class name, called class Method

// Call an object Method

[Iphone1 lookMyPhone];

Char * str = [iphone1 receiptMessage];

NSLog (@ "% s", str );

[Iphone1 sendMessage: "good bye" withPhoneNumber: "114"];

Required urn 0;

}


How does oc determine whether an object is an instance of a subclass of a class?

If ([childObject. Parent class] isKindOfClass: [Parent class]
{
}
 
What type of time is used in oc?

NSDate

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.