Objective-C

05-面向对象语法04

2020-06-09  本文已影响0人  Andy_Livings

一、继承

1.继承的基本用法
// Bird的声明
@interface Bird : NSObject
{
    @public
    int weight;
}
- (void)eat;
@end
// Bird的定义
@implementation Bird
- (void)eat {
    NSLog(@"吃吃吃-体重:%d", weight);
}
@end
// Dog的声明
@interface Dog : NSObject
{
    @public
    int weight;
}
- (void)eat;
@end
// Dog的定义
@implementation Dog
- (void)eat {
    NSLog(@"吃吃吃-体重:%d", weight);
}
@end
// Animal的声明
@interface Animal : NSObject
{
    @public
        int weight;
}
- (void)eat;
@end
// Animal的定义
@implementation Animal
- (void)eat {
    NSLog(@"吃吃吃-体重:%d", weight);
}
@end
// Bird的声明
@interface Bird : Animal
{
    @public
        int height;
}
- (void)fly;
@end

// Bird的定义
@implementation Bird
- (void)fly {
    NSLog(@"飞飞飞-高度:%d", height);
}
@end

// Dog的声明
@interface Dog : Animal
{
    @public
        int speed;
}
- (void)run;
@end
// Dog的定义
@implementation Dog
- (void)run {
    NSLog(@"跑跑跑-高度:%d", speed);
}
@end
2.继承的专业术语
3.继承的细节
4.super关键字
5.继承的好处
6.继承的使用场合

二、多态

1.多态的基本概念
2.多态的体现
Person *p = [Student new];
p->age = 100;
[p walk];
3.多态的好处
4.多态的局限性
5.多态的细节

三、NSString的简单使用

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

猜你喜欢

热点阅读