I. Class and object, Class Object

Source: Internet
Author: User

I. Class and object, Class Object
1. interface part: Declares the behaviors and features of the class (the class definition is divided into interface part and implementation part) ① @ interface Keyword: used to indicate that this is a class interface part of the function: is to define the static characteristics of the class and declare the dynamic behavior @ end as the end sign for external interface: through the interface, you can understand the class's Person: class name without knowing the implementation, each class must have a name. Uppercase letters (Class: object type) NSObject: parent class @ interface Person: NSObject ② static features: instance variable ③ the instance variable must be in curly brackets ④ it cannot be assigned when creating the instance variable ⑤ when creating the instance variable, the instance variable money plus "_": The system writes it like this; differentiation; {⑤ visibility of instance variables inside 1: class implementation using instance variable outside 2: Not in implementation using 3 @ public range: external, internal, inherit from 4 @ protected (System Default) range: Internal, inherit from 5 @ private range: Internal // Add the current class attribute
@ Public // The scope of action is until the next end or the NSString * _ name; NSInteger _ age; @ protected CGFloat _ height; @ private CGFloat _ weight;
} // Dynamic Behavior: method (adding class behavior)-(void) sayHi; @ end2. implementation part: class behavior implementation (method implementation) ① @ implementation: keyword, indicates the implementation part of the class Person: Class Name, indicating which class is implemented @ end: class end implementation flag @ implementation Person-(void) sayHi {NSLog (@ "Hello World ");
} @ End3. use the class to create an object. All objects created in Objective-C are placed in the heap area (Object: class instance) int main () {@ autoreleasepool {① address of the applied space storage space "1" Memory Allocation (alloc method: used to allocate memory space) Person * p = [Person alloc]; object initialization (init method: used to initialize objects), and clears the memory space data p = [p init]; 3. Combine "1" and "2" of the created object into one step: Person * p1 = [[Person alloc] init]; ② use the member variable p1-> _ age = 10; p1-> _ name = @ "beiye"; ③ usage [p1 sayHi];
} Return 0;
} 4. class is an abstraction of a class of things with the same behavior and characteristics. 5. encapsulation, inheritance, and polymorphism ① encapsulation: binds the attributes and actions of an object in the real world and places them in a logical unit ② inheritance: subclass automatically shares the parent data structure and method mechanism, which is a kind of relationship between classes ③ polymorphism: the same operation, function, and process can act on multiple types of objects and obtain different results. 6. @ class: It only indicates that there is such a type as Girl, and nothing else is done. When the Girl class is used, import Girl again. h note: we generally choose. use @ class in h, in. introduce the header file in m. usage of the combination class: (the pointer stores the first address of the object, representing the object. In OC, use pointers to represent objects for operations.) ① Mother. h # import <Foundation/Foundation. h>

@ Interface Mother: NSObject
{
@ Public
NSString * _ name;
} @ End ② Father. h # import <Foundation/Foundation. h>

@ Interface Father: NSObject
{
@ Public
NSString * _ name;
}

@ End ③ Child. h # import <Foundation/Foundation. h>

@ Interface Child: NSObject
{
@ Public
NSString * _ name;
} @ End ④ Family. h # import <Foundation/Foundation. h> # class Father; # class Mother; # class Child;
@ Interface Family: NSObject
{
@ Public
Father * _ f;
Mother * _ m;
Child * _ c;
}

@ End ⑤ main. m Father * father = [[Father alloc] init];
Father-> _ name = @ "father ";
Mother * mother = [[Mother alloc] init];
Mother-> _ name = @ "mother ";
Child * child = [[Child alloc] init];
Child-> _ name = @ "child ";
Family * family = [[Family alloc] init];
Family-> _ f = father;
Family-> _ m = mother;
Family-> _ c = child; NSLog (@ "% @", family-> _ f-> _ name, family-> _ m-> _ name, family-> _ c-> _ name); 8. the printed string uses "% @" as the placeholder. "[]" Is used to print the call method, and no console input 9. ① process-oriented: the programming is centered on events (tasks). The program lists every step of the event around the event, and completes it step by step (Focus: implementing function C) ② object-oriented: programming is centered on things, and the program is centered around things. Completing events is just a small task (Focus: Object Features OC, C ++, java) 10. methods in Objective-C ① "-" instance method: executed by the class instance. In other words, you must create an instance of this class before calling the instance method. The instance method is the most common method type (the method can only be used by objects) Example:-(instancetype) init; // No return value for a parameter (1 parameter)
// The colon is part of the method name.
// Method Name: printNum:
-(Void) printNum :( NSInteger) num;
// There are two parameters
// Method Name: printName: age:
-(Void) printName :( NSString *) name age :( NSInteger) age;

// There are three parameters
// Method name: printName: age: score:-(void) printName :( NSString *) name age :( NSInteger) age score :( CGFloat) score; ② "+" class method: it can be directly executed by its class. It does not need the instance of the object as the receiver of the message (the method can only be used by the class) Example: + (instancetype) alloc; Note: The instance variable 11 cannot be used in class methods. difference between instancetype and id ① instancetype can return objects of the same type as the class of the method. id can only return unknown objects. ② Instancetype can only be used as the return value and parameter. id can also be used to define the variable ③ instancetype will tell the compiler the current type, but id is non-typed for the compiler, no error is returned when any method is called. ④ for the init method, the id and instancetype are no different. Because the compiler will optimize the id to instancetype. When it is clear that the returned type is the current class, using instancetype can avoid compilation errors caused by IDs. 12. How to obtain method name ① Delete: type identifier, return type, parameter type, parameter name, space. Example: replaceObjectAtIndex: withObject Note: A method of the same name cannot appear in the class; ":" indicates the parameter and cannot be omitted. A colon must have a parameter. A colon is part of the method name. 13. use the message sending mechanism in OC: [receiver message] Description: ① send a message to the receiver object ② the receiver receives the message, that is, the method getSalary ③ receiver finds the message method and executes 14. setter and getter Methods ① In OC, the method used to assign values to a single instance variable is called setter (setter) ② the method used to obtain a single instance variable is called getter (accesser) 15. the setter and getter methods are written in the format of NSInteger _ age. ① setter:-(void) setAge :( NSInteger) age; that is, the instance variable name with uppercase letters of set + (ignore the underline) ② the writing format of getter:-(NSInteger) age; j indicates that the return value type is consistent with the variable type, the method name is the same as the instance variable name (ignore the underline) 16. relationship between setter and getter and instance variables ① both setter and getter operate on instance variables. ② each instance variable requires a pair of setter and getter Methods 17. setter and getter operations ① Person. h # import <Foundation/Foundation. h>

// Class Interface
/*
1. Define a class
2. Add the current class attribute (feature)
3. Add the current class behavior (method)
*/
@ Interface Person: NSObject
{
// Add the current class attribute inside the braces
// String type
NSString * name; // name. NSString is also a class.
// Integer
NSInteger age; // age. NSInteger is a numerical value, not a type.
// String type
NSString * gender; // gender
}
// Initialization Method
// You want to obtain an Instance Object of the Person class through the initialization method, and you want the object to have its own name attribute
-(Id) initWithName :( NSString *) _ name;

// Add the current class behavior outside the braces
-(Void) sayHi;

// Setter
-(Void) setName :( NSString *) _ name;
-(Void) setAge :( NSInteger) _ age;
-(Void) setGender :( NSString *) _ gender;

// Accessors getter
-(NSString *) getName;
-(NSInteger) getAge;
-(NSString *) getGender;

@ End ② Person. m # import "Person. h"

@ Implementation Person

-(Id) initWithName :( NSString *) _ name {

// 1. Original initialization, that is, setting the variables in the memory to zero
Self = [super init]; // send the init Method to the super object
// 2. After the original Initialization is successful, you must set the name of the current object as the form parameter.
If (self! = Nil ){
Name = _ name;
}
// 3. Return the current object
Return self;
}

// Add the current class behavior outside the braces
-(Void) sayHi {

// NSLog prints the string to the console for output, similar to printf ();
NSLog (@ "hello world ");
}

// Setter
-(Void) setName :( NSString *) _ name {
// Set attributes for the current object
Name = _ name;
}
-(Void) setAge :( NSInteger) _ age {
Age = _ age;
}
-(Void) setGender :( NSString *) _ gender {
Gender = _ gender;
}

// Accessors getter
-(NSString *) getName {
// Access the attributes of the current object
Return name;
}
-(NSInteger) getAge {
Return age;
}
-(NSString *) getGender {
Return gender;
} @ End ③ main. m # import <Foundation/Foundation. h>
# Import "Person. h" // introduce the class name int main (int argc, const char * argv []) {// 1. Create an Instance Object of the Person class
Person * person = [[Person alloc] initWithName: @ "Lewis"]; // call the method []
// 2. Send the instance message sayHi to the person
[Person sayHi];

// Modify the name and add the setter and getter)
/*
Class Name property name setter method name accesser method name
Person name-setName:-getName
*/

// 3. print the name of the object of the current person instance first
NSLog (@ "person's name is % @", [person getName]);

// 4. Modify the name of the object of the current person instance to jack
[Person setName: @ "Jack"];
NSLog (@ "person's name is % @", [person getName]);
} 18. point syntax (only valid for getter setter) s. name = @ ""; NSLog (@ "% @", s. name); [s setAge: 12]; NSLog (@ "% ld", [s age]); s. score = 99.99; NSLog (@ "%. 2f ", s. score );

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.