Objective-C syntax and objects

Source: Internet
Author: User
This section describes Objective-C classes and class objects. The method is simple to use. 1. Classes and Methods

Class declaration syntax, declares a class called MyClass, which inherits from the root class: NSObject. (The root class can be directly or indirectly inherited by all other classes .)

Is a method syntax display, the method declaration consists of the following parts: method type identifier, return type, one or more method signature keywords, and parameter type and name.

Class object variable access permission:

Corresponding code:

@interface Worker : NSObject{    char *name;@private    int age;    char *evaluation;@protected    id job;    float wage;@public    id boss;}

Entity variables without keywords. For example, char * name Is @ protected. That is to say, the default value is protected.

2. Create Class 1, class and method 2, create Class 1.1, create a project, create a file by Command + N, create class Student, inherit from NSObject1.2, and generate student. h and student. m

#import <Foundation/Foundation.h>@interface Student : NSObject@end
#import "Student.h"@implementation Student@end
1.3 add class member variables and methods to the header file
#import <Foundation/Foundation.h>@interface Student : NSObject{    NSString *studentName;    NSInteger studentAge;}-(void) printInfo;-(void) setStudentName: (NSString*) name;-(void) setAge: (NSInteger) age;-(NSString*) getStudentName;-(NSInteger) GetAge;@end
  • @ Interface Class start identifier, such as the Class in Java or C
  • @ End symbol of the class
  • Inheritance Class method: Class: Parent, the code above Student: NSObject
  • The member variables are between @ interface Class: Parent {...}
  • The default access permission for member variables is protected.
  • The class member method is behind the member variable in the format of: scope (returnType) methodName: (parameter1Type) parameter1Name;
  • Scope refers to class methods or instantiation methods. Class method starts with a + sign, and the instantiation method starts with a-sign.
1.4 methods in the implementation class

@ Implementation

# Import "Student. h "@ implementation Student-(void) printInfo {NSLog (@" Name: % @ age: % d ", studentName, studentAge);}-(void) setStudentName: (NSString *) name {studentName = name;}-(void) setAge: (NSInteger) age {studentAge = age;}-(NSString *) getStudentName {return studentName ;} -(NSInteger) GetAge {return studentAge;} @ end

 

1.5. Call the method when creating a class object.
Student * student = [[Student alloc] init]; [student setStudentName: @ "Zhang San"]; [student setAge: 10]; [student printInfo];
  • Sutdent * student = [[Sutdent alloc] init]; this line of code contains several important meanings
  • [Student alloc] calls the Student class method, which is similar to allocating memory,
  • [Object init] is a member variable that constitutes a function call and an initial class object.

Print result:

Name: Zhang San age: 10 years old

2. The instance method of the class uses multiple parameters.
....-(void) setNameAndAge:(NSString*) name setAge:(NSInteger) age;....
....-(void) setNameAndAge:(NSString*) name setAge:(NSInteger) age{    studentName = name;    studentAge = age;}....
Student * student = [[Student alloc] init]; [student setNameAndAge: @ "" setAge: 20]; [student printInfo]; [student release];
3. Custom constructor 3.1 declaration and implementation Constructor
....-(Student*) initWithNameAndAge:(NSString*) name setAge:(NSInteger) age;....
....-(Student*) initWithNameAndAge:(NSString*) name setAge:(NSInteger) age{    self = [super init];        if ( self ) {        [self setNameAndAge:name setAge:age];    }        return self;}....

-(Id) init: This method is used for class initialization and creation. Each class needs to call the init method when it is created, and the self is obtained by using the init method of the parent class, in this way, we can initialize some sub-classes.

3.2 use a custom constructor:
 

Student * student = [[Student alloc] initWithNameAndAge: @ "rongfzh" setAge: 6];
[Student printInfo];
[Student release];
[Student release];

 

 

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.