4 What is a class, object, instance variable, method

Source: Internet
Author: User

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

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.