First, the curriculum review
1. Object-oriented language
Object-Oriented Programming: focus on things
Process-oriented programming: event-centric
2. Classes and objects
Description class: Abstraction of something with the same characteristics and behavior
An object is an instance of a class, and a class is an abstraction of an object
3. Defining classes
Interface section: Declaring a class's characteristics (instance variables) and behavior (methods)
Implementation part: Implementation method
4. Create objects
Person * P1 = [[Person alloc] init];
Alloc class methods only classes can call
Function: Allocates storage space for the object in the heap area, empties the storage space, sets all instance variables to 0, and returns the first address of the object storage space.
Init instance methods only objects can be called
Role: set for instance variables
P1 essence: A pointer to the person type Object
When a pointer to an object type points to the created object, the P-generation is referred to as the object
5. Object Invocation Method
[P Sayhi];
Person * P2 = nil;
P2 cannot be referred to as an object, is a null pointer
In OC, any operation on nil is invalid.
Second, the instance variable operation
When you invoke an instance variable of the public adornment outside the class, which defines a part other than the class, use-
1. Assignment of instance variables
P->_name = @ "remote star Dream";
P->_age = 18;
2. Read the value of the instance variable
NSLog (@name =%@, age =%d ", p->_name, P->_age);
Output: name = Remote Star Dream, age = 18
3. Overriding Init initialization
1) Declaration Part-----Person.h
-(person *) init;
-(ID) init; -(Instancetype) init;----General wording
ID generic pointer type, can only point to object type
notation: ID Teacher * person * cannot represent int *
2) Implement part------PERSON.M
-(ID) init
{
_age = 18;
return self;
Self means that the method of initialization must return
}
4. Example method
1) Declaration part------Person.h
-(void) introduce;
2) Implement part------PERSON.M
-(void) introduce
{
NSLog (@ "My name is%@", _name);
All created person objects can call instance methods that belong to this object
instance method, you can use the instance object directly
Which object invokes the instance method, the value in the instance variable is the value of the current object
}
3) Call-------MAIN.M
[P introduce];
Output: My name is the dream of a distant star
This article is from the OC Learning Notes blog, so be sure to keep this source http://10613955.blog.51cto.com/10603955/1684482
First lecture, Class and Object-------(2)