zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    51nod 1087 1 10 100 1000(找规律+递推+stl)
    51nod 1082 与7无关的数 (打表预处理)
    51 nod 1080 两个数的平方和
    1015 水仙花数(水题)
    51 nod 1003 阶乘后面0的数量
    51nod 1002 数塔取数问题
    51 nod 1001 数组中和等于K的数对
    51 nod 1081 子段求和
    51nod 1134 最长递增子序列 (O(nlogn)算法)
    51nod 1174 区间中最大的数(RMQ)
  • 原文地址:https://www.cnblogs.com/lsfv/p/9127977.html
Copyright © 2011-2022 走看看