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