• objective-c 快速学习1-类和继承


    参考快速学习指南

    为了快速掌握oc的基本语法,按照上面文章速度学习oc .大概用了4~5天时间。粗越了解下oc.只是粗越了解。

    目的主要还是实践 这个 “学习理论”。并完善这个学习理论。

    鬼知道以后还要学习多少语言呢?

    1.数字处理:四则运算。

    2.字符串处理:分割和组合。

    3.集合处理:可变和不可变集合。 dictionary(oc没写例子)

    4.对象和继承:人,雇员,老板, 放入集合并多态的表示他们的薪水和税收。

    5.意图和实现的分离(事件):单个函数指针的实现(c风格和block)。一组函数指针的实现(protocal)

    6.io的处理。没写

    7.内存管理

    //
    //  main.m
    //  practice
    //
    //  Created by liangshun on 18/5/30.
    //  Copyright © 2018年 liangshun. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    @protocol PPerson <NSObject>
    -(NSString *) Walk;
    -(NSString *) Eat;
    -(float) PayTax;
    @end
    
    @interface basePerson : NSObject
    @property NSString* name;
    @property float money;
    @end
    @implementation basePerson
    -(id)init{
        self=[super init];
        if(self!=nil)
        {
            self.name=@"";
            self.money=100;
        }
        return self;
    }
    -(id)init_with:(float)m
    {
        if(self=[super init])
        {
            self.name=@"";
            self.money=m;
        }
        return self;
    }
    
    @end
    
    
    @interface Employee : basePerson <PPerson>
    
    @end
    
    @implementation Employee
    -(NSString *)Walk
    {
        return [NSString stringWithFormat:@"%@ is walking.",self.name];
    }
    -(NSString *)Eat
    {
        return [NSString stringWithFormat:@"%@ is eating.",self.name];
    }
    -(float) PayTax
    {
        return self.money*0.1;
    }
    @end
    
    
    @interface Boss : basePerson <PPerson>
    
    @end
    
    @implementation Boss
    -(NSString *)Walk
    {
        return [NSString stringWithFormat:@"%@ has a car !",self.name];
    }
    -(NSString *)Eat
    {
        return [NSString stringWithFormat:@"%@ is eating good food.",self.name];
    }
    -(float) PayTax
    {
        return self.money*0.2;
    }
    
    -(void) GetMoney
    {
        NSLog(@"boss get money");
    }
    
    @end
    
    
    @interface RoadPeoele : NSObject
    @property NSString* name;
    -(float) PayTax;
    @end
    
    @implementation RoadPeoele
    -(float) PayTax
    {
        return 0;
    }
    @end
    
    
    
    void showMeTax(id<PPerson> p)
    {
        NSLog(@"执行接口方法:%f",[p PayTax]);
    }
    
    
    int main(int argc, const char * argv[]) {
        Employee *andy=[[Employee alloc ] init_with:40];
        andy.name=@"Andy";
        
        Boss *linson=[[Boss alloc] init_with:100];
        linson.name=@"linson";
        
        RoadPeoele *tt=[RoadPeoele new];
        tt.name=@"TT";
    
        //宽松的多态-multablearrays。可以放入任何类型
        NSMutableArray *allworker=[NSMutableArray new];
        [allworker addObject:linson];
        [allworker addObject:andy];
        [allworker addObject:tt];
        for(int i=0;i<[allworker count];i++)
        {
            NSLog(@"%@,pay tax:%f",[allworker[i] name],[allworker[i] PayTax]);
        }
        //宽松的多态-array 可以放入任何类型
        NSArray *allp=[NSArray arrayWithObjects:linson, andy,tt, nil];
        for(int i=0;i<[allp count];i++)
        {
            NSLog(@"%@,pay tax:%f",[allp[i] name],[allp[i] PayTax]);
        }
        //宽松的多态-<T> 可以放入任何类型!!!!!!why?
        NSArray<PPerson> *allp2=[NSArray<PPerson> arrayWithObjects:linson, andy,tt, nil];
        for(int i=0;i<[allp2 count];i++)
        {
            NSLog(@"%@,pay tax!!!:%f",[allp2[i] name],[allp2[i] PayTax]);
            showMeTax(allp2[i]);
        }
        return 0;
    }
  • 相关阅读:
    Java开发中回车换行符的区别
    Eclipse中快捷键使用
    数组基础常用方法
    获取一组数组中最大值和最小值
    解决Java工程URL路径中含有中文的情况
    python录音,无声自动停止,或定时停止
    ChatterBot人工智能,聊天机器人,无坑指南(安装,使用)(2.使用篇)
    ChatterBot机器学习,聊天机器人,无坑指南(安装,使用)(1.安装篇)
    python播放mp3最佳方法
    自动天气
  • 原文地址:https://www.cnblogs.com/lsfv/p/9127977.html
Copyright © 2020-2023  润新知