#import "ViewController.h" #import "ClassA.h" #import "BaseClass.h" #import "SubClass.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. ClassA *a = [ClassA new]; // BaseClass *class = [BaseClass new]; SubClass *sub = [SubClass new]; /**打印 initialize繼承規則 BaseClass initialize SubClass initialize * */ //initialize 方法只應該用來設置內部數據,不該該在其中調用其餘方法,幾遍是本類本身的方法,也最好別調用 }
#import <Foundation/Foundation.h> @interface ClassA : NSObject @end #import "ClassA.h" #import "ClassB.h" @implementation ClassA //load 僅調用一次,而且不遵照繼承規則,如今OC中不須要使用它 +(void)load{ NSLog(@"load ClassA"); ClassB *b = [ClassB new]; } @end
#import <Foundation/Foundation.h> @interface ClassB : NSObject @end #import "ClassB.h" @implementation ClassB @end
#import <Foundation/Foundation.h> @interface BaseClass : NSObject @end #import "BaseClass.h" static const int kInterval = 10; static NSMutableArray *array; @implementation BaseClass +(void)initialize{ NSLog(@"%@ initialize",self); if (self == [BaseClass class]){ array = [NSMutableArray array];//全局狀態跟在編譯器初始化 } } @end #import "BaseClass.h" @interface SubClass : BaseClass @end #import "SubClass.h" @implementation SubClass @end