Objective-C基础——面向对象语法04


一、继承


1.继承的使用场合
 1> 当两个类拥有相同属性和方法的时候,就可以将相同的东西抽取到一个父类中
 2> 当A类完全拥有B类中的部分属性和方法时,可以考虑让B类继承A类

 A
 {
    int _age;
    int _no;
 }
 
 B : A
 {
    int _weight;
 }
 // 继承:xx 是 xxx
 // 组合:xxx 拥有 xxx
 
 2.组合
 A
 {
     int _age;
     int _no;
 }
 
 B
 {
     A *_a;
     int _weight;
 }

例如
@interface Score : NSObject
{
    int _cScore;
    int _ocScore;
}
@end

@implementation Score
@end

@interface Student : NSObject
{
    // 组合
    Score *_score;
//    int _cScore;
//    int _ocScore;
    int _age;
}
@end

@implementation Student

@end

 1.继承的好处:
 1> 抽取重复代码
 2> 建立了类之间的关系
 3> 子类可以拥有父类中的所有成员变量和方法
 
 2.注意点
 1> 基本上所有类的根类是NSObject


#import <Foundation/Foundation.h>

/********Animal的声明*******/
@interface Animal : NSObject
{
    int _age;
    double _weight;
}

- (void)setAge:(int)age;
- (int)age;

- (void)setWeight:(double)weight;
- (double)weight;
@end

/********Animal的实现*******/
@implementation Animal
- (void)setAge:(int)age
{
    _age = age;
}
- (int)age
{
    return _age;
}

- (void)setWeight:(double)weight
{
    _weight = weight;
}
- (double)weight
{
    return _weight;
}
@end

/********Dog*******/
// : Animal 继承了Animal,相当于拥有了Animal里面的所有成员变量和方法
// Animal称为Dog的父类
// Dog称为Animal的子类
@interface Dog : Animal
@end

@implementation Dog
@end

/********Cat*******/
@interface Cat : Animal
@end

@implementation Cat
@end

int main()
{
    Dog *d = [Dog new];
    
    [d setAge:10];
    
    NSLog(@"age=%d", [d age]);
    return 0;
}

 1.重写:子类重新实现父类中的某个方法,覆盖父类以前的做法
 2.注意
 1> 父类必须声明在子类的前面
 2> 子类不能拥有和父类相同的成员变量
 3> 调用某个方法时,优先去当前类中找,如果找不到,去父类中找
 
 2.坏处:耦合性太强

#import <Foundation/Foundation.h>
// Person
@interface Person : NSObject
{
    int _age;
}

- (void)setAge:(int)age;
- (int)age;

- (void)run;

+ (void)test;

@end

@implementation Person

+ (void)test
{
    NSLog(@"Person+test");
}

- (void)run
{
    NSLog(@"person---跑");
}

- (void)setAge:(int)age
{
    _age = age;
}
- (int)age
{
    return _age;
}
@end

// 不允许子类和父类拥有相同名称的成员变量
// Student
@interface Student : Person
{
    int _no;
    // int _age;
}

+ (void)test2;

@end

@implementation Student
// 重写:子类重新实现父类中的某个方法,覆盖父类以前的做法
- (void)run
{
    NSLog(@"student---跑");
}

+ (void)test2
{
    [self test];
}
@end


int main()
{
    [Student test2];
    
//    Student *s = [Student new];
//    
//    [s run];
    
    return 0;
}
3. 继承的细节
1> 单继承
2> 子类和父类不能有相同的成员变量
3> 方法的重写
 
4. super关键字
分别调用父类的对象方法和类方法
super的作用
 1.直接调用父类中的某个方法
 2.super处在对象方法中,那么就会调用父类的对象方法
   super处在类方法中,那么就会调用父类的类方法
 3.使用场合:子类重写父类的方法时想保留父类的一些行为

// 僵尸
@interface Zoombie : NSObject
- (void)walk;

+ (void)test;
- (void)test;

@end

@implementation Zoombie
- (void)walk
{
    NSLog(@"往前挪两步******");
}

+ (void)test
{
    NSLog(@"Zoombie+test");
}

- (void)test
{
    NSLog(@"Zoombie-test");
}
@end

// 跳跃僵尸
@interface JumpZoombie : Zoombie
+ (void)haha;
- (void)haha2;
@end


@implementation JumpZoombie

+ (void)haha
{
    [super test];
}

- (void)haha2
{
    [super test];
}

- (void)walk
{
    // 跳两下
    NSLog(@"跳两下");
    
    // 走两下(直接调用父类的walk方法)
    [super walk];
    //NSLog(@"往前挪两步----");

}
@end

int main()
{
    //[JumpZoombie haha];
    JumpZoombie *jz = [JumpZoombie new];
    
    [jz haha2];
    
    return 0;
}

二、多态

1. 多态的基本概念
1> 某一类事物的多种形态
2> OC对象具有多态性


2. 多态的体现
Person *p = [Student new];  
  
p->age = 100;  
  
[p walk];  


 
子类对象赋值给父类指针
父类指针访问对应的属性和方法
 
3. 多态的好处
用父类接收参数,节省代码
 
4. 多态的局限性
不能访问子类的属性(可以考虑强制转换)
 
5. 多态的细节
动态绑定:在运行时根据对象的类型确定动态调用的方法

三、NSString的简单使用

1. 字符串的快速创建
NSStirng *str = @“Hello”;  
2. 使用静态方法创建
3. 使用%@输出字符串
NSString *name = @”Heaven”;  
  
NSLog(@“我的名字是%@”,  name);  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值