MySubClass.h
#import "MySuperClass.h"
@interface MySubClass : MySuperClass{
@private
int subVariable1;
@private
int subVariable2;
int subVariable3;
}
@property int subVariable3;
- (void)subMethod1;
- (int)subMethod2;
- (void)subMethod3:(int)a:(int)b:(int)c:(int)d:(int)e:(int)f;
- (void)subMethod4:(MySubClass*)instance;
- (MySubClass*)subMethod5;
- (void)subMethod6;
- (void)subMethod7;
+ (void)subMethod8;
- (void)subSetter:(int)a;
- (int)subGetter;
+ (void)overRideMethod;
@end
MySubClass.m
#import "MySubClass.h"
@implementation MySubClass
@synthesize subVariable3;
- (void)subMethod1 {
NSLog(@"subMethos1が読み込まれました。");
NSLog(@"インスタンス変数は%d、%d、%d、%d、%d、%d",
superVariable1,superVariable2,superVariable3,subVariable1,subVariable2,subVariable3);
return;
}
- (int)subMethod2 {
NSLog(@"subMethos2が読み込まれました。");
return 2;
}
- (void)subMethod3:(int)a:(int)b:(int)c:(int)d:(int)e:(int)f {
NSLog(@"subMethos3が読み込まれました。");
self->superVariable1 = a;
self->superVariable2 = b;
self.superVariable3 = c;
self->subVariable1 = d;
self->subVariable2 = e;
self.subVariable3 = f;
return;
}
- (void)subMethod4:(MySubClass*)instance {
NSLog(@"subMethos4が読み込まれました。");
self->superVariable1 = instance->superVariable1 + 2;
self->superVariable2 = instance->superVariable2 + 2;
self.superVariable3 = instance->superVariable3 + 2;
self->subVariable1 = instance->subVariable1 + 2;
self->subVariable2 = instance->subVariable2 + 2;
self.subVariable3 = instance->subVariable3 + 2;
return;
}
//クラス内ならカプセル化されたインスタンス変数へもアロー演算子でアクセスできる
//また、スーパークラスのカプセル化されたインスタンス変数へもアクセスできる
//インスタンスに関しては、メッソド内で変更、生成したものはメソッド実行後も残る
- (MySubClass*)subMethod5 {
NSLog(@"subMethos5が読み込まれました。");
MySubClass *instance = [[MySubClass alloc] init];
instance->superVariable1 = self->superVariable1;
instance->superVariable2 = self->superVariable2;
instance->superVariable3 = self.superVariable3;
instance->subVariable1 = self->subVariable1;
instance->subVariable2 = self->subVariable2;
instance->subVariable3 = self.subVariable3;
return instance;
}
//サブクラスでスーパークラスを利用できる
- (void)subMethod6 {
NSLog(@"subMethos6が読み込まれました。");
[super superMethod1];
return;
}
//メソッド内で、自分のクラスやインスタンスを用いる際はselfを使う
//selfはクラスメソッド内では自分のクラスをさし、インスタンスメソッド無いでは自分自身をさす。
- (void)subMethod7 {
NSLog(@"subMethos7が読み込まれました。");
[self subMethod3:10 :10 :10 :10 :10 :10];
NSLog(@"%d",self.subVariable3);
return;
}
//super
+ (void)subMethod8 {
[self overRideMethod];
[super overRideMethod];
return;
}
//セッターと同じ意味のメソッド
- (void)subSetter:(int)a {
NSLog(@"subSetterが読み込まれました。");
subVariable2 = a;
return;
}
//ゲッターと同じ意味のメソッド
- (int)subGetter {
NSLog(@"subGetterが読み込まれました。");
return subVariable2;
}
//オーバーライド
+ (void)overRideMethod {
NSLog(@"サブクラスから読み込まれたクラスメソッドです。");
return;
}
@end
MySuperClass.h#importMySuperClass.m@interface MySuperClass : NSObject{ @public int superVariable1; @protected int superVariable2; int superVariable3; } @property int superVariable3; - (void)superMethod1; + (void)overRideMethod; @end
#import "MySuperClass.h"
@implementation MySuperClass
@synthesize superVariable3;
- (void)superMethod1 {
NSLog(@"superMethod1が読み込まれました。");
return;
}
//オーバーライド
+ (void)overRideMethod {
NSLog(@"スーパークラスから読み込まれたクラスメソッドです。");
return;
}
@end
main.m#import#import "MySubClass.h" int main(int argc, const char * argv[]) { @autoreleasepool { MySubClass *subInstanceA = [[MySubClass alloc] init]; MySubClass *subInstanceB = [[MySubClass alloc] init]; MySubClass *subInstanceC = [[MySubClass alloc] init]; MySuperClass *superInstanceA = [[MySuperClass alloc] init]; [subInstanceA subMethod1]; [subInstanceA subMethod2]; [subInstanceA subMethod3:1 :2 :3 :4 :5 :6]; [subInstanceA subMethod1]; [subInstanceB subMethod4:subInstanceA]; [subInstanceB subMethod1]; //戻り値にインスタンス型のメソッドを入れ子に //インスタンスに関しては、メッソド内で変更・生成したものは、メソッド実行後も残る [[subInstanceA subMethod5] subMethod1]; //サブクラスの宣言ファイルを読み込んでいれば、スーパークラスも利用できる [superInstanceA superMethod1]; [subInstanceA subMethod6]; //オーバーライド、スーパークラスのメソッドをサブクラスのメソッドで上書きできる [MySuperClass overRideMethod]; [MySubClass overRideMethod]; //self [subInstanceA subMethod7]; //super [MySubClass subMethod8]; //セッターとゲッター [subInstanceC subSetter:3]; NSLog(@"セッターより得られた値は%dです。",[subInstanceC subGetter]); [subInstanceC setSubVariable3:4]; NSLog(@"セッターより得られた値は%dです。",[subInstanceC subVariable3]); subInstanceC.subVariable3 = 5; NSLog(@"セッターより得られた値は%dです。",subInstanceC.subVariable3); } return 0; }
Result
コメント