——黑马程序员——OC中的继承

-----------android培训java培训、java学习型技术博客、期待与您交流!-----------

OC中的继承

一、继承概念

继承:子类(派生类)拥有了父类同样的方法和属性(实例变量)过程,继承是指类和类之间的关系,子类可以对父类的行为进行扩展、覆盖和重定义

二、继承的优点:

1、省去大量的代码

2、使类和类之间建立起了联系

例如:

//动物继承NSObject 类

@interface Animal: NSObject

{

}

-(void) run;

@end


//类的实现

@implementation Animal

-(void) run{

NSLog(@"狗正在跑");

}

@end


//创建狗的类

@interface Dog:Ainmal

-(void)lookhome;

@

//狗类的实现

@implementation Dog

-(void)lookhome{


NSLog(@"狗正在瞪着眼睛看家");


}

@end

#import <Foundation/Foundation.h>

//动物继承NSObject 类
@interface Animal: NSObject
{
	
}
-(void) run;
@end

//类的实现
@implementation Animal
-(void) run{
NSLog(@"动物正在跑");
}
@end

//创建狗的类
@interface Dog: Animal
-(void)lookhome;
@end
//狗类的实现
@implementation Dog
-(void)lookhome{

NSLog(@"狗正在瞪着眼睛看家");

}
@end

int main (int argc, const char *argv[]) 
{ 
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];                         
   
    //匿名类调用方法
    [[Animal new] run ];
    [[Dog new] lookhome ];
    
    //创键对象
    Dog *d = [Dog new];
    //让Dog调用run的方法
    [d run];
   
    [pool drain];
    return 0;
}


<span style="font-size:14px;">#import <Foundation/Foundation.h>

//动物继承NSObject 类
@interface Animal: NSObject
{

NSString * _name;
int _age;	
}
//定义set get方法
-(void)setName:(NSString *) name;
-(void)setAge:(int) age;
-(NSString *)name;
-(int)age; 
//定义run方法
-(void) run;
@end

//类的实现
@implementation Animal
//set get方法的实现
-(void)setName:(NSString *) name{
	_name = name;	
	}
-(void)setAge:(int) age{
	_age = age;
	}
-(NSString *)name{
	return _name;
	}
-(int)age{
	return _age;
	}

-(void) run{
NSLog(@"动物岁正在跑");
}
@end

//创建狗的类
@interface Dog: Animal
-(void)lookhome;
@end
//狗类的实现
@implementation Dog
-(void)lookhome{

NSLog(@"狗正在瞪着眼睛看家");

}
@end

int main (int argc, const char *argv[]) 
{ 
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];                         
   

    [[Dog new] lookhome ];
    
    //创键对象
    Animal *a = [Animal new];
    Dog *d = [Dog new];
    
    //用set方法给动物实例变量赋值
    [a setName: @"脊椎动物"];
    [a setAge: 100];
    
    NSLog(@"%@%d岁",[a name],[a age]);
    //调用方法
    [a run];
    
    //给狗设置名称和年龄  狗类本身没哟实力变量_name,_age,因为Dog继承了父类,父类有
    [d setName: @"小黄"];
    [d setAge: 5];
    //让d调用run的方法

    NSLog(@"这个狗叫%@今年%d岁", [d name], [d age]);
    [d run];
    [pool drain];
    return 0;
}</span>


三、基类和派生类之间的关系

1、派生类不但拥有基类的方法和属性,还可以自己增加属性和方法

2、基类的私有属性能被继承但是不能被访问

3、OC中的继承是单一继承,一个类只能继承一个父类

4、要注意继承的合理性,只有一类的可以继承

四、方法的重写(重点)

当子类从父类继承方法不适合子类时,可以在子类中重写父类的方法
执行过程是:先调用自己子类中的方法,如果自己类中没有方法再去父类中查找,如果父类中依然没有该方法则程序报错

#import <Foundation/Foundation.h>

//动物继承NSObject 类
@interface Animal: NSObject
{

NSString * _name;
int _age;	
}
//定义set get方法
-(void)setName:(NSString *) name;
-(void)setAge:(int) age;
-(NSString *)name;
-(int)age; 
//定义run方法
-(void) run;
@end

//类的实现
@implementation Animal
//set get方法的实现
-(void)setName:(NSString *) name{
	_name = name;	
	}
-(void)setAge:(int) age{
	_age = age;
	}
-(NSString *)name{
	return _name;
	}
-(int)age{
	return _age;
	}

-(void) run{
NSLog(@"动物正在跑");
}
@end

//创建狗的类
@interface Dog: Animal
-(void)lookhome;
@end
//狗类的实现
@implementation Dog
-(void)lookhome{

NSLog(@"狗正在瞪着眼睛看家");

}

<strong><span style="color:#ff0000;">//重写父类Animal的方法,
-(void) run{
NSLog(@"狗正在跑");</span></strong>
}

@end

int main (int argc, const char *argv[]) 
{ 
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];                         
   

    [[Dog new] lookhome ];
    
    //创键对象
    Animal *a = [Animal new];
    Dog *d = [Dog new];
    
    //用set方法给动物实例变量赋值
    [a setName: @"脊椎动物"];
    [a setAge: 100];
    
    NSLog(@"%@%d岁",[a name],[a age]);
    //调用方法
    [a run];
    
    //给狗设置名称和年龄  狗类本身没哟实力变量_name,_age,因为Dog继承了父类,父类有
    [d setName: @"小黄"];
    [d setAge: 5];
    //让d调用run的方法

    NSLog(@"这个狗叫%@今年%d岁", [d name], [d age]);
    //当dog类调用run 时 显示的是动物在跑,我们想要让他显示狗再跑,因此就需要方法重写
    [d run];
    [pool drain];
    return 0;
}


五、继承使用的注意事项

1、子类不能定义和父类同名的变量,因为子类已经继承了基类的属性和方法,相同时程序报错,

2、OC中只能单一继承,不支持多继承

3、OC中支持多层继承

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章