OC Introductory First Lesson

Source: Internet
Author: User

OC Introductory First Lesson


HELLO.M (Implementation) hello.c
#import <Foundation/Foundation.h> Introduction of header Files
#include <F......>
Import is an enhanced version of include, pre-processing only once
Compile Link: clang hello.m-framework Foundation
(-fobjc-arc compiled with automatic reference count)//terminal default is manual reference count
(-fno-objc-arc compiled with manual reference count)


class declaration: Only methods and variables can be declared (may be used in Chinese characters, but generally with letters, it is recommended to start with _)
@interface car:nsobject//car Inheritance NSObject
@end
Implementation of the class: implementation of the method in the interface
@implementation Car
When the object is destroyed, the system automatically calls
(-instance, member method, must instance call: Car. Method name)/(+ class method, class name Call) (return value type void) (method name) dealloc{//similar to C + + destructor
NSLog (@ "%s", __func__);//__function__
The manual reference count must be called, and the automatic reference count cannot be called
[Super Dealloc];
}
@end


/***/
The size of the class cannot be measured with sizeof
Car Car;//error


OC class types can only be used to define pointers//oc all objects are allocated on the heap, so only pointers can be defined, and variables of ordinary types cannot be defined
Car *car = nil;//initialization, nil represents an empty object


Create object (OC allocates memory)
Car *car = [Car alloc];//[] represents a call to a method, with malloc in C
Initializing objects
[Car init];


New is equivalent to encapsulating the Alloc and Init
Car *car = [car new];


Releasing objects
[Car release];


Two types of reference counts
MRC (Manual reference counting manual reference count)
ARC (Automatic reference counting auto reference count)
ARC cannot use release and Super Dealloc and will be automatically recycled
The MRC uses


The variables defined in OC are initialized automatically
Access permission settings: can only be used on variables
protected by default: @protected (self and subclass available)
Public: @public
Private: @private (only for your own use)
Example:
@private float _h;
Char _c;//ibid. _c is a private variable


The precedence of the variable at the beginning of the _ is higher than the variable with the same name but no underscore
such as: _money priority higher than money


/***/function Pointers:
typedef void (*FUNC) ();
void Y () {
NSLog (@ "xxx");
}
Func x () {
return y;
}


X () ();//xxx


Macros that are common in OC:
__line__
The following three outputs print the same
__function__
__pretty_function__
__func__


Specification for OC Coding: Hump Naming method
Class Name: initial capital Cartest
(instance, member) variable: underscore begins with _h
Normal variable: F


Definitions of different properties:
PROPERTY//OC-specific rules
attribute//member variables, properties of objects


OC does not have a static member variable (i.e. without a + start)
Instance (instance, Object-object) method, member method
-(void) test;
Class method
+ (void) test;
The colon is also part of the method name
-(void) test3;
-(void) Test3: (int) A;
-(void):(int) A;
[Car test3];
[Car test3:234];
[car:456];


A class function cannot be manipulated to modify a variable in a class, and the member function can


Nnstring *_name;//String type, assigned @ "";
%@//nnstring Type output format


Encapsulation (contract format)
. h
@interface Car:nsobject
{
@private
int _money;
}
-(void) Setmoney: (int) money;
-(int) money;
@end


. m
-(void) print{
NSLog (@ "print");
}
-(void) Setmoney: (int) money{
_money = money;
[Self print];//method in the call class
}
-(int) money
{
return _money;
}


Main ()
{
Car *car = [car new];
car.money=12;//Call Setmoney
NSLog (@ "%d", car.money);//Call Money
}


Inheritance: Implementing reusability
OC supports single inheritance (only one parent class)
Access to the parent class private variable by self. Variable name


Polymorphic: Different subclasses of the same parent class can overload methods of the parent class to implement a function that is not the same
You can point to the object of the child class with a pointer to the parent class, but the child pointer cannot point to the object of the parent class, depending on the pointer type
Collegestu *stu = [[Student alloc] init];(wrong)
Student *STU1 = [Collegestu new];(right)
Summary: College students must be students, but students are not necessarily college students


Compile-Time type: type of pointer
Run-Time type: The type that is actually pointed to (run-time binding)
[STU1 release]; Set the pointer to empty (ST1 = nil;)

OC Introductory First Lesson

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.