zoukankan      html  css  js  c++  java
  • [Objective-C] 004_继承封装与多态

    继承

      面向对象编程 (OOP) 语言的一个主要功能就是"继承"。继承是指这样一种能力:它可以使用现有类的所有功能,并在无需重新编写原来的类的情况下对这些功能进行扩展。通过继承创建的新类称为"子类"或"派生类",被继承的类称为"基类"、"父类"或"超类"。继承的过程,就是从一般到特殊的过程。在考虑使用继承时,有一点需要注意,那就是两个类之间的关系应该是"属于"关系。例如,Teacher 是一个人,Student 也是一个人,因此这两个类都可以继承 Person 类。但是 Leg 类却不能继承 Person 类,因为腿并不是一个人。

    Person类

    /////////////////    .h    ////////////////
    #import <Foundation/Foundation.h>
    
    @interface Person : NSObject
    
    @property (nonatomic,copy)NSString *name;
    @property (nonatomic,assign)int age;
    @property (nonatomic,assign)NSString *sex;
    
    - (void)printInfo;
    @end
    
    
    /////////////////    .m    ////////////////
    #import "Person.h"
    
    @implementation Person
    @synthesize name = _name,sex = _sex;
    @synthesize age = _age;
    
    - (void)printInfo {
        NSLog(@"我的名字叫:%@ 今年%d岁 我是一名%@ %@",self.name,self.age,self.sex,NSStringFromClass([self class]));
    }
    @end
    

      

      

    Teacher 类

    /////////////    .h   //////////// 
    #import "Person.h"
    
    @interface Teacher : Person
    
    @end
    
    
    /////////////    .m   //////////// 
    #import "Teacher.h"
    
    @implementation Teacher
    
    - (void)printInfo {
        NSLog(@"我的名字叫:%@ 今年%d岁 我是一名%@ %@",self.name,self.age,self.sex,NSStringFromClass([self class]));
    }
    @end
    

      

    封装
      封装是对象和类概念的主要特性。它是隐藏内部实现,稳定外部接口,可以看作是"包装"。 封装,也就是把客观事物封装成抽象的类,并且类可以把自己的数据和方法只让可信的类或者对象操作,对不可信的进行信息隐藏。

    Student 类

    ////////////////    .h    ///////////
    #import "Person.h"
    
    @interface Student : Person
    
    - (id)initWithName:(NSString *)name sex:(NSString *)sex age:(int)age;
    @end
    
    
    ////////////////    .m    ///////////
    #import "Student.h"
    
    @implementation Student
    
    - (id)initWithName:(NSString *)name sex:(NSString *)sex age:(int)age
    {
        self = [super init];
        if (self) {
            self.name = name;
            self.sex = sex;
            self.age = age;
        }
        return self;
    }
    
    - (void)printInfo {
        NSLog(@"我的名字叫:%@ 今年%d岁 我是一名%@ %@",self.name,self.age,self.sex,NSStringFromClass([self class]));
    }
    @end
    

      

    多态
      多态性(polymorphism)是允许你将父对象设置成为和一个或更多的他的子对象相等的技术,赋值之后,父对象就可以根据当前赋值给它的子对象的特性以不同的方式运作。简单的说,就是一句话:允许将子类类型的指针赋值给父类类型的指针。不同对象以自己的方式响应相同的消息的能力叫做多态。意思就是假设生物类(life)都用有一个相同的 方法-eat;
那人类属于生物,猪也属于生物,都继承了life后,实现各自的eat,但是调用是我们只需调用各自的eat方法。
也就是不同的对象以 自己的方式响应了相同的消息(响应了eat这个选择器)。
    实现多态,有二种方式,覆盖(是指子类重新定义父类的虚函数的做法),重载(是指允许存在多个同名函数,而这些函数的参数表不同(或许参数个数不同,或许参数类型不同,或许两者都不同))。

    Cleaner 类

    ///////////////////    .h    //////////
    #import "Person.h"
    
    @interface Cleaner : Person
    
    @end
    
    ///////////////////    .m    //////////
    #import "Cleaner.h"
    
    @implementation Cleaner
    
    - (void)printInfo {
        NSLog(@"我的名字叫:%@ 今年%d岁 我是一名%@ %@",self.name,self.age,self.sex,NSStringFromClass([self class]));
    }
    
    - (void)work {
        NSLog(@"我在扫地");
    }
    
    - (void)work:(NSString *)tool {
        NSLog(@"我在用%@扫地",tool);
    }
    @end
    

    AppDelegate.m 中测试

    #import "AppDelegate.h"
    #import "Teacher.h"
    #import "Student.h"
    #import "Person.h"
    #import "Cleaner.h"
    
    @interface AppDelegate ()
    
    @end
    
    @implementation AppDelegate
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        
        Person *p = [[Person alloc] init];
        p.name = @"隔壁老王";
        p.age = 36;
        p.sex = @"男";
        
        Teacher *t = [[Teacher alloc] init];
        t.name = @"CJK";
        t.age = 28;
        t.sex = @"女";
        
        Student *s = [[Student alloc] initWithName:@"小明" sex:@"男" age:12];
        
        Cleaner *c = [[Cleaner alloc] init];
        c.name = @"程燕飞";
        c.age = 48;
        c.sex = @"女";
        
        [p printInfo];
        [t printInfo];
        [s printInfo];
        [c printInfo];
        [c work];
        [c work:@"魔法扫帚"];
        
        return YES;
    }
    
    - (void)applicationWillResignActive:(UIApplication *)application {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }
    
    - (void)applicationDidEnterBackground:(UIApplication *)application {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }
    
    - (void)applicationWillEnterForeground:(UIApplication *)application {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }
    
    - (void)applicationDidBecomeActive:(UIApplication *)application {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }
    
    - (void)applicationWillTerminate:(UIApplication *)application {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }
    
    @end

    测试结果:

    2015-06-01 17:50:34.174 Attendance[54400:1327317] 我的名字叫:隔壁老王今年36我是一名男 Person

    2015-06-01 17:50:34.175 Attendance[54400:1327317] 我的名字叫:CJK 今年28我是一名女 Teacher

    2015-06-01 17:50:34.175 Attendance[54400:1327317] 我的名字叫:小明今年12我是一名男 Student

    2015-06-01 17:50:34.175 Attendance[54400:1327317] 我的名字叫:程燕飞今年48我是一名女 Cleaner

    2015-06-01 17:50:34.175 Attendance[54400:1327317] 我在扫地

    2015-06-01 17:50:34.175 Attendance[54400:1327317] 我在用魔法扫帚扫地

     

    本站文章为 宝宝巴士 SD.Team 原创,转载务必在明显处注明:(作者官方网站: 宝宝巴士 
    转载自【宝宝巴士SuperDo团队】 原文链接: http://www.cnblogs.com/superdo/p/4544543.html

     

     

  • 相关阅读:
    java volatile关键字解惑
    Java 反射
    拷贝源实体类到目标实体类中
    Bean和Map之间的转换
    DateUtils时间的封装
    HttpClient的代码封装,便于直接调用
    HttpClient语法
    LinkedHashMap+ConcurrentHashMap+hashMap的区别
    1006 Tick and Tick
    Event Flow
  • 原文地址:https://www.cnblogs.com/superdo/p/4544543.html
Copyright © 2011-2022 走看看