OC Class Essay--Class

Source: Internet
Author: User

1. Definition of class

class = attribute + method

--attributes represent the characteristics of a class

--the method is the reaction of the class to the change

The format of a class definition: the declaration of a class and the implementation of a class

--Interface (Declaration of Class): @interface class Name: The name of the base class

The first letter of the class name should be capitalized.

The. Colon represents an inheritance relationship, and the class after the colon is the parent class

. NSObject is the base class for all classes

The declaration of the. class is placed in the class name +.h file, which consists of two parts: the instance object and the method declaration.

--Implementation (Implementation of Class): @implementation class name

. Method implementation

All ends with @

2. Use of Classes

Objects in the. OC are declared by pointers. such as Classa*object;

The creation of objects in OC, using Alloc to create an object. The compiler assigns an available memory address to the object. You then need to initialize the object to call the Init method so that the object can be used.

Such as:

Person *person=[person alloc];

Person=[person Init];

Or it can be written as:

Person *person=[[person alloc] init];

Each object of the same class has different storage spaces for instance variables, and each object of the same class shares its methods.

The. method is nested in the form of a call, such as: ClassA *object=[[classa alloc] init];

3. Access rights for classes

. Three access rights

--The public @public

--The private @private

--The @protect of protection

The default permissions for properties are protected;

Method The default permissions are public;

4. Definition of the method

. No parameter no return-(void) print;

. No reference and return-(person *) register;

Parametric non-return-(void) init: (int) newId;

There is a return-(person *) init: (int) newId;

5. Definition of Multi-parameter method

Method with one parameter

--(person *) init: (int) newId;

Methods for two parameters

-(Person *) init: (int) newId andage :(int) newage;

: is part of the method name

-First method Name: init:

-second method name: init:andage :

-(Person *) Initwithid: (int) NewID andage: (int) newage Andname: (NSString *) name;

You can precede the method name with arbitrary tags such as "Withid", "Andage", "Andname" and so on

6. Comparison of class methods and Object methods

. Object methods (instance methods)

--start with a minus sign

--only objects can be called, no objects, methods cannot be executed

--Object methods can access instance variables (member variables)

Class method

--Start with a plus + +

--can only be called with the class name, the object cannot be called

--Class methods cannot access instance variables

--Application: When you do not need to access member variables, use the class method as much as possible

--Class methods and object methods can have duplicate names

Instance:

MAIN.M:

#import <Foundation/Foundation.h>#import"Dog.h"int main (int argc,Constchar *Argv[]) {@autoreleasepool {//Create an object, that is, instantiate the object Dog *hashiqi=[[dog Alloc]init]; // method call  [Hashiqi bark]; // hashiqi-> Health=120@ " Husky " ; NSLog (@ " Dog's name:%@, health value%d health);} return 0   

DOG.M: Implementation of classes

Thedeclaration file for the "Dog.h"     // Reference class @implementation the Dog// Method implementation-(void) bark{NSLog ( @ " barking ");} @end              

Dog.h: Declarations of classes

#import <Foundation/Foundation.h>@interface dog:nsobject   // Declare class Dog, inherit the Foundation Class NSObject//  The properties of the class   // settings class are public (not generally allowed) NSString *int health ;} // Object Method-(void) bark;

7. How to access the property:

Gets the definition of the property value (Getter) method

--Function: Returns the member variable inside the object

--Naming specification: The name of the Get method is generally the same as the member variable;

Gets the definition of the property value (Setter) method

--Role: Used to set member variables, you can filter out some unreasonable values in the method

--Naming specification:

--the method starts with a set, followed by the member variable name, and the first letter must be capitalized

--parameter names do not have the same name as member variables

8.getter and Setter Method examples

Main

#import <Foundation/Foundation.h>#import"Dog.h"int main (int argc,Constchar *Argv[]) {@autoreleasepool {// Create an object, that is, instantiate the object Dog *hashiqi=[[dog Alloc]init]; // method call  [Hashiqi bark]; //@ " Husky "]; // call setter method Assignment [Hashiqi sethealth:120@ " Dog's name:%@ Health Status:%d  ",hashiqi.name,hashiqi.health);} return 0    

DOG.M: Implementation of classes

#import"Dog.h"//Declaration file for a reference class@implementationDog//Method Implementation-(void) bark{NSLog (@"Barking");}//Setter Method-(void) SetName: (nsstring *) newname{Name=newname;} //getter Method-(NSString * ) name{return name; //setter Method-(void) Sethealth: (intnewhealth;} //getter Method-(int) health{return health;}  @end             

Dog.h: Declarations of classes

#import <Foundation/Foundation.h>@interface Dog:nsobject//Declaring class dog, inheriting the foundation class NSObject The property of the//class {NSString *name; int health ;} // Object Method-(void) bark;-(void) SetName: (NSString *) newName; //Setter Method-(NSString *) name; //getter Method-(void) Sethealth: (int) newhealth;-(int) health;  @end                    

9. How to access automatically generated attributes

@property syntax

@synthesize

Benefits of Encapsulation

--Filter out unreasonable values

--the assignment process inside the Shield

--Let the outside world not focus on internal details

10. Example of an access method that automatically generates attributes

Main

#import <Foundation/Foundation.h>#import"Dog.h"int main (int argc,Constchar *Argv[]) {@autoreleasepool {// Create an object, that is, instantiate the object Dog *hashiqi= [[dog Alloc]init]; // [Hashiqi bark]; //@ " Husky " ]; [Hashiqi sethealth:120@ " Dog's name:%@ Health Status:%d  ",hashiqi.name,hashiqi.health);} return 0; 
/span>
DOG.M: implementation of the Class
 #import dog.h" // @implementation  Dog // method implementation-(void) bark{NSLog (@ " bark );}  @synthesize Name,health; // package instead of Setter,getter  @end 

Dog.h: Declarations of Classes
#import <Foundation/Foundation.h> @interface dog:nsobject //  Declare class dog, inherit the base class Nsobject// { @public; // Set the properties of the class to public nsstring *name; int health;} -(voidint Health; // package instead of Setter,getter@property nsstring *name;  @end 

11. Method overloads
· Not a strict function overload in OC

Naming conflicts:

-(int) dosomething (int) X;

-(int) dosomething (float) X;

Naming conflicts:

-(int) dosomething (int) x: (int) y;

-(int) dosomething (float) x: (int) y;

No conflict:

-(int) dosomething (int) x: (int) y;

-(int) dosomething (int) X:andy: (float) y;

Because one is "dosomething:", "The Other is dosomething::"

12. Inheritance

• Do not change the original model based on the extension charge method

• Establish a link between classes and classes

• Extraction of common code

• Reduce redundant code

• Disadvantages: Strong coupling

OC Class Essay--Class

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.