OC Basic Syntax

Source: Internet
Author: User

Header file

1 @interfacestudent:nsobject{2     intAge ;3     intheight;4}//the declaration interval for the member variable, which must be declared here5 6- (int) Age;//it was getage, but OC's habit was to name the Get method with a variable.7- (void) Setage: (int) NewAge;8 //the function of multi-shape parameter is very special.9- (void) Setage: (int) NewAge Andheight: (int) Newheight;Ten @end//the end tag of the class must be written

the corresponding m files are :

1 #import "Student.h"2 @implementationStudent3 4- (int) age{5     returnAge ;6 }7- (void) Setage: (int) newage{8Age =NewAge;9 }Ten- (void) Setage: (int) NewAge Andheight: (int) newheight{ OneAge =NewAge; AHeight =Newheight; - } - @end

object creation and method invocation :

//OC Create objects in 2 steps, first call the static parameterless function Alloc request memory, call the static parameterless function init initialization//1. Student *stu = [Student alloc];//just to divide the object into memory space//2. Stu = [Stu init];//really create objects//the above 2 steps are generally abbreviated as:Student *stu =[[Student alloc] init];//Set[Stu Setage: -]; [Stu Setage: -Andheight: -];//GetNSLog (@"Age is %i", [Stu Age]); [Stu release];//object is finished using to free memory

How to construct an object

1 @interfacestudent{2     int_age;//Standard notation3     int_no;4 }5- (void) Setage: (int) age;6- (int) age;7- (void) Setno: (int) No;8- (int) No;9 //Construction MethodTen- (ID) Initwithage: (int) Age Andno: (int) No; One @end

The corresponding M file:

1#include"Student.h"2 @implementationStudent3 4- (int) age{5     return_age;6 }7- (void) Setage: (int) age{8_age =Age ;9 }Ten //... One //Implementing a Construction method A- (ID) Initwithage: (int) Age Andno: (int) no{ -     //The following wording is not rigorous -     //Self = [super init]; the     //_age = age; -     //_no = no; -     if(self=[Super Init]) { -_age =Age ; +_no =No; -     } +     returnSelf ; A } at @end

OC Basic Syntax

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.