In object-oriented programming, a general OC program consists of three files:
. h file. m file main.mFile. At compile time, Xcode compiles all files on the. M.. h file: This is a header file, which can also be said to be an interface section. The declarations of instance variables (properties of a Class), Object methods, and class methods are all in this file.
- If you define a "people" class, Person.h
@interface person:nsobject//Attribute (instance variable) {NSString *_name;//name int _age;//Age}//object method declaration (behavior)-(void) eat; Meal-(void) SetName: (NSString *) name; Name setter-(NSString *) name; Name getter-(void) Setage: (int) age; Age setter-(int) ages; Declaration of the class method + (void) Run: (person *) P1; Running @end
. m file: This is the implementation part of the class. Is the interface part of the code implementation, to copy the interface part of the header file #import "Person.h".
Code:#import "Person.h" #import <Foundation/Foundation.h> @implementation Implementation of person//object methods//eating-(void) eat{NSLog (@ " Eat full "); [Person run]; You can call the set method of the class method}//name and Get Method-(void) SetName: (NSString *) name{_name = name;} -(NSString *) name{return _name;} The set method of age and the Get Method-(void) Setage: (int) age{_age = age;} -(int) age{return _age;}//class method implementation//Run + (void) Run: (person *) p1{[P1 eat];//Can use object method, but to pass object parameter NSLog (@ "I'm Running");} @end
main.m file int main () {//create object person *p = [person new]; Set the value of the instance variable [P setname:@ "Mrzeng"]; [P setage:26]; Read instance variable NSLog (@ "My name is:%@, age is:%i", [p name], [P]); class method [Person Run:p]; Object method [P eat]; return 0;}
4 What is a class, object, instance variable, method