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;
    }
  • 相关阅读:
    《闯关东》群英传
    这老太太
    URL重写与伪静态
    创建索引视图时提示架构绑定无效,名称必须由两部分构成
    马色见
    食神智多星
    Beautiful Code and Beautiful Software
    /wp64 Compiler Option
    C++的x64移植
    Managing the State Data of MFC Modules
  • 原文地址:https://www.cnblogs.com/lsfv/p/9127977.html
Copyright © 2011-2022 走看看