OBJECTIVE-C----Instance Variables

Source: Internet
Author: User

One, the visibility of instance variables (access rights)

Second, the method

There are two types of methods in OC: Class, method and instance? method.

Class? Method: Can only be used for class, for example: + (ID) Alloc Note: Class? Method cannot be used in the instance variable

Instance? Method: Only objects can be used, for example:-(void) Sayhi

Third, direct sticker code
////MAIN.M//Oc_practice_02////Created by on 15/3/31.//Copyright (c) 2015. All rights reserved.//#import <Foundation/Foundation.h> #import "Person.h" #import "AudiCar.h" #import "Car.h" #import "Engine.h" #import "Tire.h" intMainintargcConst Char* argv[]) {Person *aperson = [[Person alloc] init];//Assign a value to its related propertyAperson->_address = @"Zhengzhou High-tech zone Lotus Pond ditch son"; Aperson->_hobby = @"Knock Code";the assignment and accessor methods in a class are defined to address the form of protected and private instance variables that cannot be accessed directly by an object and pointed to an operator. belongs to indirect access. //[Aperson setname:@ "Henan Province"];//[Aperson setage:12];//[Aperson setsex:@ "female"];[Aperson setname:@"Ha"Age atsex:@"Hei"] ;NSLog( @"\n%@,%d,%@", [Aperson GetName], [Aperson getage], [Aperson getsex]);The custom initialization method is to assign a value to the corresponding instance variable when the object is created, and the setter method assigns the corresponding instance variable after the object is created, both of which can be assigned, but the timing of the assignment is different.     The //initialization method can only be called once when the object is created, and the assignment method may be called multiple times depending on the specific needs of the project. Person *anotherperson = [[Person alloc] initwithname:@"Heihei"Age $sex:@"NV"] ;NSLog(@"\nname:%@, age:%d, sex:%@", [Anotherperson GetName], [Anotherperson getage], [Anotherperson getsex]); Person *fiveperson = [[Person alloc] initwithname:@"en"Age atsex:@"en"address:@"Ehh"hobby:@"Hen"] ;NSLog(@"\nname:%@, age:%d, sex:%@, address:%@, hobby:%@", [Fiveperson GetName], [Fiveperson getage], [Fiveperson Getsex], fiveperson->_addr    ESS, Fiveperson->_hobby); Audicar *onecar = [[Audicar alloc] initwithcolor:@"ee"Price1.3HorsePower:2.2type:@"ee"] ;NSLog(@"color:%@, price:%.2f, horsepower:%.2f, type:%@", Onecar->_color, Onecar->_price, Onecar->_horsepower, Onecar->_type);//Create engine ObjectEngine *anengine = [[Engine alloc] initwithbrand:@"V8"HorsePower: -] ; Car *acar = [[Car alloc] init];//For the current car object installation engine[ACar Setengine:anengine]; for(inti =0; I <4; i++) {Tire *atire = [[Tire alloc] initwithbrand:@"Michelin"Size in] ;    [ACar Settire:atire atindex:i]; } [ACar run];return 0;}

OBJECTIVE-C----Instance Variables

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.