I. Classes and objects

Source: Internet
Author: User

1. Interface part: The behavior and characteristics of the foreign declaration class (the definition of class is divided into: interface part and implementation part)

① @interface interface keyword: Used to indicate that this is an interface part of a class       Interfaces section features: is a static feature that defines a class and declares dynamic behavior       @end as the closing flag            external interface: through the interface can not know the implementation of the case, understand what this class has       person: Class name, each class will have a name. Initial capitalization (class: Type of object)      nsobject: Parent class @interface person:  Nsobject② static Characteristics: Instance variables ③ instance variables must be ④ when creating instance variables in curly braces ⑤ when creating an instance variable, the instance variable money Plus "_": the system writes; distinguishes the visibility of;{     ⑤ instance variables       Internal to 1: Class implementations use instance variables       2: Not in the implementation section       "3" @public scope: external, internal, inheritance       4 @protected (System default) Scope: Internal, inheritance       5 @private scope: Internal      //add current class properties       @public//scope is until the next end or the instance variable ends      nsstring *_name;     nsinteger _age;        @protected      cgfloat _height;       @private      cgfloat _weight;}  //Dynamic Behavior: Method (Add Class behavior)-(void) sayhi;  @end2. Implementation part: Implementation of the class behavior (Implementation of the method) ① @implementation: keyword that represents the implementation part of the class   &nbsp  person: Class name, which indicates which class is implemented       @end: class end Implementation flag @implementation person -(void) sayhi{     nslog (@ "Hello World");}   @end3. Create an object using a class. Objective-c objects created in the heap area (object: Instance of Class) int main () {      @autoreleasepool {          ① application space           Save space address           1 memory allocation (Alloc method: For allocating memory space)     & nbsp     person *p = [person alloc];          ' 2 ' Object Initialization (Init method: Used to initialize the object), zeroing the memory space data           p = [p init];           3 "1" and "2" will be created to create a step   & nbsp       person *p1 = [person alloc] init];          ② using member variables   & nbsp       P1->_age = 10;          p1->_name = @ "bei ye";     &NB Sp    ③ How to use           [P1&NBSP;SAYHI];&NBSP;&NBSP;&NBSP;&Nbsp; }     return 0;} 4. A class is an abstraction of something that behaves and features the same thing 5. Encapsulation, inheritance, polymorphic ① encapsulation: The property and behavior of an object existing in the real world are bound together and placed within a logical unit ② inheritance: a mechanism by which subclasses automatically share data structures and methods of the parent class, This is a relationship between classes ③ polymorphism: Refers to the same operation or function, the process can be used on multiple types of objects and get different results [email protected] role: Just to show that there is girl such a type, the other did nothing. Where to use girl this class, then import Girl.h Note: We generally choose to use the @class in. h to introduce a header file in. M 7. Use of the Combined class: (The pointer stores the object's first address, substituting the object.) Use pointers to refer to objects in OC to operate) ①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. Print the string with "%@" as a placeholder. Call methods with "[]" to print, no console input 9.①-oriented process: Programming with events (tasks) as the center, the program around the event, listing the events of each step, step by step (focus: implementation of function C) ② Object-oriented: programming to the center of things, the program around things unfold, Completing an event is just a small task (focus: The object's feature Oc,c++,java) 10. Several methods in Objective-c ① "-" instance method: executed by an instance of the class. In other words, you must first create an instance of the class before invoking the instance method. Instance methods are the most common method type (methods can only be used by objects) Example:-(instancetype) init;//with parameter (one parameter) no return value
A colon is part of a 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: Can be executed directly by the class in which it resides. It does not require an instance of the object as the recipient of the message (the method can only be used by the Class) example: + (instancetype) alloc; Note: Class methods cannot use instance variables 11.instancetype and ID differences ①instancetype can return objects of the same type as the method's class, and the ID can only return an unknown type object. ②instancetype can be used only as a return value and as a parameter, and the ID also defines the variable ③instancetype tells the compiler the current type, but the ID is untyped to the compiler, and calling any method does not give an error ④ for the Init method, There is no difference between the ID and the instancetype. Because the compiler will optimize the ID to instancetype. When the type that is explicitly returned is the current class, the use of instancetype avoids the compiler error caused by the ID. 12. How to get the method name ① Delete: Type ID, return type, parameter type, parameter name, space. Example: Replaceobjectatindex:withobject Note: A method with the same name cannot appear in a class; ":" identifies a parameter and cannot be omitted. There must be an argument for the colon, and the colon is part of the method name. 13. Use the message sending mechanism in OC: [receiver message] expression: ① sends a message message to receiver object ②receiver receive the messages, that is, the method Getsalary③receiver find the Message method, and perform the 14.setter and Getter methods ① in OC, the method of assigning a value to a single instance variable is called a setter (set) ② gets a singleton variable that is worth the method called getter (accessor) 15.setter and the write format of the Getter method to the instance variable: Nsinteger _age;①setter's Writing format:-(void) Setage: (Nsinteger) age; The name of the instance variable (ignoring underscore) in the first uppercase of the set+ the written format of ②getter:-(Nsinteger) age; J that is, the return value type is the same as the variable type, the method name is the same as the instance variable name (omit underscore) 16.setter and the Getter's relationship to the instance variable ① whether the setter or getter internal operation is an instance variable ② each instance variable requires a pair of setter andGetter Method 17.setter and getter of the actual operation ①person.h#import <foundation/foundation.h>

Interface part of a class
/*
1. Definition of a class
2. Add the current class attribute (feature)
3. Adding the current class behavior (method)
*/
@interface Person:nsobject
{
Inside the curly brace, add the current class property
String type
NSString *name;//name, NSString is also a class
Integral type
Nsinteger age;//Age, Nsinteger is a numeric type, not a class
String type
NSString *gender;//Sex
}
Initialize method
You want to be able to get an instance object of the person class through the initialization method, and you want the object to have its own name property
-(ID) Initwithname: (NSString *) _name;

Outside curly braces, adding the current class behavior
-(void) sayhi;

Set setter
-(void) SetName: (NSString *) _name;
-(void) Setage: (Nsinteger) _age;
-(void) Setgender: (NSString *) _gender;

Accessor getter
-(NSString *) getName;
-(Nsinteger) getage;
-(NSString *) Getgender;

@end ②person.m#import "Person.h"

@implementation person

-(ID) Initwithname: (NSString *) _name{

1. Original initialization, that is, the in-memory variables are zeroed
self = [Super init];//sends Init method to Super Object
2. After the original initialization is successful, you need to set the name of the current object as the parameter
if (self! = nil) {
name = _name;
}
3. Returning the current object
return self;
}

Outside curly braces, adding the current class behavior
-(void) sayhi{

NSLog prints the string to the console for output, similar to printf ();
NSLog (@ "Hello World");
}

Set setter
-(void) SetName: (NSString *) _name{
Property set to the current object
name = _name;
}
-(void) Setage: (Nsinteger) _age{
age = _age;
}
-(void) Setgender: (NSString *) _gender{
gender = _gender;
}

Accessor getter
-(NSString *) getname{
Accessing the properties 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 class name int main (int argc, const char * argv[]) {//1. Create an instance object of the person class
Person *person = [[Person alloc] initwithname:@ "Lewis"];//with "" Call method
2. Send instance messages to person Sayhi
[Person Sayhi];

Modify name, add Setter (setters) and accessor (getter)
/*
Class Name Property name Set method name accessor method name
Person Name-setname:-getname
*/

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

4. Modify the name of the current person instance object to change to Jack
[Person setname:@ "Jack"];
NSLog (@ "person ' s name is%@", [person getName]);
}18. dot syntax (valid only for getter setters) S.name = @ "Small white";       NSLog (@ "%@", s.name); [s setage:12]; NSLog (@ "%ld", [s Age]); s.score = 99.99; NSLog (@ "%.2f", S.score);

I. Classes and objects

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.