老谢博客

  • 首页
  • WordPress
  • 网络技术
  • 乱七八糟
  • 运维技术
  • 给我留言
  • 关于老谢

Objective-C 中面向对象基础笔记

分类:程序设计日期:2015-10-21 - 23:22:55作者:老谢

  Objective-C下创建类以后,会出现两个文件,分别后缀为.h和.m,其中.h一个是类的声明(@interface),一个是类的实现(@implementation),在创建一个类时,我们可以在main.m中 main()方法之前,作出声明与实现:

People.h
#import <Foundation/Foundation.h>
 
@interface People : NSObject
@property(nonatomic,strong)NSString *peopleName;
-(void)report;
@end

#import <Foundation/Foundation.h> @interface People : NSObject @property(nonatomic,strong)NSString *peopleName; -(void)report; @end

  在这里我创建了一个People类,继承自NSObject父类,声明了一个peopleName属性,并声明了一个无返回值类型的成员方法report方法,特别注意的是-号表示成员方法,如果是+号则表示类方法。

People.m
#import "People.h"
 
@implementation People
{
    NSString *_peopleName;
    int _peopleAge;
}
-(void)report{
    NSLog(@"report方法被调用了");
}
@end

#import "People.h" @implementation People { NSString *_peopleName; int _peopleAge; } -(void)report{ NSLog(@"report方法被调用了"); } @end

  前面说过了,.m是对方法的实现,那么我们的方法要写在.m的源文件里面,在这里我声明了一个字符串类型的_peopleName和一个int类型的_peopleAge两个成员变量,前面加上下划线_是为了表示其为成员变量,而不是属性,成员变量仅限在方法内部使用,而属性可以被外部调用,在.h中我们使用了@property(nonatomic,strong)NSString *peopleName;来创建了一个peopleName的字符串类型的属性,如果在.m里面我即便没有声明_peopleName这个成员变量,Xcode也会自动帮我生成一个字符串类型的_peopleName成员变量!

  然后在report方法里面,我用NSLog打印了一句话。

main.m
#import <Foundation/Foundation.h>
#import "People.h"
 
int main(int argc, const char * argv[]) {
    @autoreleasepool {
 
        People *p1 = [[People alloc]init];
        p1.peopleName = @"张三";
        //NSLog(@"p1 - %p",p1);
        NSLog(@"p1 - %@",p1.peopleName);
 
    }
    return 0;
}

#import <Foundation/Foundation.h> #import "People.h" int main(int argc, const char * argv[]) { @autoreleasepool { People *p1 = [[People alloc]init]; p1.peopleName = @"张三"; //NSLog(@"p1 - %p",p1); NSLog(@"p1 - %@",p1.peopleName); } return 0; }

  在main()中,通过People *p1 = [[People alloc]init];来实例化对象p1,其中alloc是一个类方法用来分配对象的内存空间,init是一个成员方法用来初始化对象,成员方法只能被对象所使用,所以alloc的返回值其实是一个对象。

  以为我们在People.h中声明了peopleName属性,所以可以通过属性p1.peopleName = @”张三”;来对_peopleName这个成员变量直接赋值。

方法中参数的传递

  还是上面的代码,在People.h里面声明两个成员方法,代码如下:

#import <Foundation/Foundation.h>
 
@interface People : NSObject
@property(nonatomic,strong)NSString *peopleName;
-(void)report;
-(int)showWithA:(int)a;
-(int)showWithA:(int)a andB:(int)b;
@end

#import <Foundation/Foundation.h> @interface People : NSObject @property(nonatomic,strong)NSString *peopleName; -(void)report; -(int)showWithA:(int)a; -(int)showWithA:(int)a andB:(int)b; @end

  分别是返回值为int类型的showWithA:成员方法,参数为int类型的形参a,另外一个是名为showWithA: andB:的成员方法,接着我们就要在People.m里面实现我们声明的方法:

People.m
#import "People.h"
 
@implementation People
{
    NSString *_peopleName;
    int _peopleAge;
}
-(void)report{
    NSLog(@"peopleName = %@",_peopleName);
}
-(int)showWithA:(int)a{
    return a;
}
-(int)showWithA:(int)a andB:(int)b{
    return a+b;
}
@end

#import "People.h" @implementation People { NSString *_peopleName; int _peopleAge; } -(void)report{ NSLog(@"peopleName = %@",_peopleName); } -(int)showWithA:(int)a{ return a; } -(int)showWithA:(int)a andB:(int)b{ return a+b; } @end

  这里没什么好说的,showWithA:方法返回的是a,showWithA: andB:返回的是a+b的值,下面我们在main()里面调用看看:

main.m
#import <Foundation/Foundation.h>
#import "People.h"
 
int main(int argc, const char * argv[]) {
    @autoreleasepool {
 
        People *p1 = [[People alloc]init];
        //p1.peopleName = @"张三";
        //NSLog(@"p1 - %p",p1);
        //NSLog(@"p1 - %@",p1.peopleName);
        //[p1 report];
 
        int a1 = [p1 showWithA:20];
        NSLog(@"a1 = %d",a1);
 
        int a2 = [p1 showWithA:10 andB:20];
        NSLog(@"a2 = %d",a2);
 
    }
    return 0;
}

#import <Foundation/Foundation.h> #import "People.h" int main(int argc, const char * argv[]) { @autoreleasepool { People *p1 = [[People alloc]init]; //p1.peopleName = @"张三"; //NSLog(@"p1 - %p",p1); //NSLog(@"p1 - %@",p1.peopleName); //[p1 report]; int a1 = [p1 showWithA:20]; NSLog(@"a1 = %d",a1); int a2 = [p1 showWithA:10 andB:20]; NSLog(@"a2 = %d",a2); } return 0; }

  在这里通过创建的实例对象名分别调用成员方法并传递实参给成员方法,并分别赋值给int型的a1和a2进行打印。

对象初始化操作

  之前我们通过init方法进行对象的初始化操作,也可以通过重写init方法进行对象初始化操作,在People.h里面声明-(instancetype)init;方法:

Peole.h
#import <Foundation/Foundation.h>
 
@interface People : NSObject
@property(nonatomic,strong)NSString *peopleName;
-(int)report;
-(int)showWithA:(int)a;
-(int)showWithA:(int)a andB:(int)b;
-(instancetype)init;
@end

#import <Foundation/Foundation.h> @interface People : NSObject @property(nonatomic,strong)NSString *peopleName; -(int)report; -(int)showWithA:(int)a; -(int)showWithA:(int)a andB:(int)b; -(instancetype)init; @end

  然后在People.m里面对init方法进行重写:

People.m
- (instancetype)init
#import "People.h"
 
@implementation People
{
    NSString *_peopleName;
    int _peopleAge;
}
-(int)report{
    NSLog(@"peopleName - %@",_peopleName);
    NSLog(@"peopleAge - %d",_peopleAge);
    return _peopleAge;
}
-(int)showWithA:(int)a{
    return a;
}
-(int)showWithA:(int)a andB:(int)b{
    return a+b;
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        _peopleAge = 20;
    }
    return self;
}
@end

- (instancetype)init #import "People.h" @implementation People { NSString *_peopleName; int _peopleAge; } -(int)report{ NSLog(@"peopleName - %@",_peopleName); NSLog(@"peopleAge - %d",_peopleAge); return _peopleAge; } -(int)showWithA:(int)a{ return a; } -(int)showWithA:(int)a andB:(int)b{ return a+b; } - (instancetype)init { self = [super init]; if (self) { _peopleAge = 20; } return self; } @end

  在main()里面调用report方法,可以发现peopleAvg的值已经被我们重写的init方法初始化为20:

main.m
#import <Foundation/Foundation.h>
#import "People.h"
 
int main(int argc, const char * argv[]) {
    @autoreleasepool {
 
        People *p1 = [[People alloc]init];
        p1.peopleName = @"张三";
        //NSLog(@"p1 - %p",p1);
        //NSLog(@"p1 - %@",p1.peopleName);
        //[p1 report];
 
        //int a1 = [p1 showWithA:20];
        //NSLog(@"a1 = %d",a1);
 
        //int a2 = [p1 showWithA:10 andB:20];
        //NSLog(@"a2 = %d",a2);
 
        int a1 = [p1 report];
        NSLog(@"a = %d",a1);
 
    }
    return 0;
}

#import <Foundation/Foundation.h> #import "People.h" int main(int argc, const char * argv[]) { @autoreleasepool { People *p1 = [[People alloc]init]; p1.peopleName = @"张三"; //NSLog(@"p1 - %p",p1); //NSLog(@"p1 - %@",p1.peopleName); //[p1 report]; //int a1 = [p1 showWithA:20]; //NSLog(@"a1 = %d",a1); //int a2 = [p1 showWithA:10 andB:20]; //NSLog(@"a2 = %d",a2); int a1 = [p1 report]; NSLog(@"a = %d",a1); } return 0; }

  输出结果:

2015-10-22 00:05:02.789 Demo[4602:197580] peopleName – 张三
2015-10-22 00:05:02.790 Demo[4602:197580] peopleAge – 20
2015-10-22 00:05:02.790 Demo[4602:197580] a = 20

自定义初始化方法

  在People.h声明自定义的初始化方法-(instancetype)initWithPeopleName:(NSString *)peopleName andPeopleAge:(int)peopleAge;

  在People.m里面实现自定义方法:

-(instancetype)initWithPeopleName:(NSString *)peopleName andPeopleAge:(int)peopleAge{
    self = [super init];
    if(self){
        _peopleName = peopleName;
        _peopleAge = peopleAge;
    }
    return self;
}

-(instancetype)initWithPeopleName:(NSString *)peopleName andPeopleAge:(int)peopleAge{ self = [super init]; if(self){ _peopleName = peopleName; _peopleAge = peopleAge; } return self; }

  然后在main()里创建对象进行初始化并调用成员方法:

        People *p2 = [[People alloc]initWithPeopleName:@"张三" andPeopleAge:30];
        People *p3 = [[People alloc]initWithPeopleName:@"李四" andPeopleAge:40];
 
        [p2 report];
        [p3 report];

People *p2 = [[People alloc]initWithPeopleName:@"张三" andPeopleAge:30]; People *p3 = [[People alloc]initWithPeopleName:@"李四" andPeopleAge:40]; [p2 report]; [p3 report];

  输出结果:

2015-10-22 00:55:32.773 Demo[4737:203387] peopleName – 张三
2015-10-22 00:55:32.774 Demo[4737:203387] peopleAge – 30
2015-10-22 00:55:32.774 Demo[4737:203387] peopleName – 李四
2015-10-22 00:55:32.774 Demo[4737:203387] peopleAge – 40

原文地址 : https://www.xj123.info/6302.html

本站遵循 : 署名-非商业性使用-相同方式共享 2.5 中国大陆 (CC BY-NC-SA 2.5)

版权声明 : 原创文章转载时,请务必以超链接形式标明文章原始出处

Tags: IOS , Objective-C
  • 上一篇:JAVA 基础学习笔记整理
  • 下一篇:Foundation框架常用方法学习笔记
0条评论

暂时没有评论!

发表评论 点击取消评论.

*必填

*必填

  • 文章归档
  • 子网计算
  • 我的共享
  • 锻炼计划
  • 给我留言
  • 关于老谢
2023年 2月
一 二 三 四 五 六 日
 12345
6789101112
13141516171819
20212223242526
2728  
« 10月    

最新文章

  • 2021好久没更新博客
  • Zabbix监控oxidized备份状态
  • Zabbix 5.0 LTS版本MySQL表分区及编译安装随记
  • centos7.9部署oxidized自动备份交换机配置
  • Surface Pro 4更换屏幕与电池
  • VCSA中删除horizon view链接克隆生成的replica-受保护副本
  • Esxi6.7U3安装SanDisk Fusion-io 1.3T ioscale Pci SSD加速卡驱动
  • 搭建ELK日志系统分析处理fortigate的syslog日志
  • 华为USG防火墙配置NAT映射回流解决内网通过公网映射访问内部服务器
  • 飞塔防火墙fortitoken配置

最新评论

  • waids:#网络金融专家! 看看新的机器人。 ht...
  • waids:机器人从不睡觉。 它为你赚钱24/7。 h...
  • waids:没有钱? 在线赚取。 https://iujxnsp...
  • waids:只需点击一下即可将您的美元变成$1000...
  • waids:需要钱吗? 金融机器人是你的解决方案...
  • waids:开始每周只用这个机器人赚几千美元。 ...
  • waids:现在启动金融机器人开始赚钱。 https:...
  • waids:没有钱? 在这里在线赚取它们很容易。...
  • RainH:大佬,这个111.111.111.111是什么鬼?...
  • 王叨叨:幸福的人都是相似的,不幸的人各有各...

日志存档

  • 2021年10月
  • 2021年4月
  • 2021年3月
  • 2021年2月
  • 2020年11月
  • 2020年9月
  • 2020年5月
  • 2020年4月
  • 2020年3月
  • 2020年1月
  • 2019年12月
  • 2019年10月
  • 2019年7月
  • 2019年6月
  • 2019年5月
  • 2019年3月
  • 2019年1月
  • 2018年12月
  • 2018年11月
  • 2018年10月
  • 2018年7月
  • 2018年6月
  • 2018年5月
  • 2018年4月
  • 2018年3月
  • 2018年1月
  • 2017年10月
  • 2017年9月
  • 2017年8月
  • 2017年7月
  • 2017年2月
  • 2017年1月
  • 2016年12月
  • 2016年11月
  • 2016年10月
  • 2016年7月
  • 2016年6月
  • 2016年4月
  • 2016年2月
  • 2016年1月
  • 2015年12月
  • 2015年10月
  • 2015年9月
  • 2015年7月
  • 2015年5月
  • 2015年4月
  • 2015年3月
  • 2015年2月
  • 2015年1月
  • 2014年12月
  • 2014年10月
  • 2014年9月
  • 2014年8月
  • 2014年7月
  • 2014年6月
  • 2014年5月
  • 2014年4月
  • 2014年3月
  • 2014年2月
  • 2014年1月
  • 2013年12月
  • 2013年11月
  • 2013年10月
  • 2013年9月
  • 2013年8月
  • 2013年7月
  • 2013年6月
  • 2013年5月
  • 2013年4月
  • 2013年3月
  • 2013年2月
  • 2013年1月
  • 2012年12月
  • 2012年11月
  • 2012年9月
  • 2012年8月
  • 2012年7月
  • 2012年6月
  • 2012年5月
  • 2012年4月
  • 2012年3月
  • 2012年2月
  • 2012年1月
  • 2011年12月
  • 2011年11月
  • 2011年10月
  • 2011年9月
  • 2011年8月
  • 2011年7月
  • 2011年6月
  • 2011年5月
  • 2011年4月
  • 2011年3月
  • 2011年2月
  • 2011年1月
  • 2010年12月
  • 2010年11月
  • 2010年10月
  • 2010年9月
  • 2010年8月
  • 2010年7月

W3C

  • XHTML 1.0 Transitional
  • CSS level 3
  • Google+
Copyright © 2010-2022 老谢博客 All rights reserved.
Gzipped 76.5% | Optimized loading 58 queries in 0.273 seconds | Memory 33.86 MB | 皖ICP备13010663号-1
Powered by WordPress. | Hosted By 腾讯云 | Theme by WordPress主题巴士 | 站点地图 | SiteMap | Uptime | 技术支持:苏州天剑计算机系统有限公司