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