老谢博客

  • 首页
  • 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条评论

暂时没有评论!

发表评论 点击取消评论.

*必填

*必填

  • 文章归档
  • 子网计算
  • 我的共享
  • 锻炼计划
  • 给我留言
  • 关于老谢
2019年二月
一 二 三 四 五 六 日
« 1月    
 123
45678910
11121314151617
18192021222324
25262728  

最新文章

  • Zabbix 数据库备份
  • Radware LinkProof 8016 负载均衡器开机主板E0错误修复
  • 终于买了张满意的桌子
  • Zabbix监控Bind(DNS)解析状态状态
  • Zabbix监控Apache性能
  • Zabbix通过PSK共享密钥实现Server和Agent的通信加密
  • Grafana 5.3 安装及配置
  • Surface Pro 4分辨率过高导致远程桌面过小的问题
  • 修复Surface Pro 4幽灵触摸故障
  • laoxie.me

最新评论

  • 搬瓦工vps:学习了
  • 大D:最近谢总更新很勤快呀
  • huka:我修好了 太感谢博主了,我的是surface...
  • huang:多谢老哥
  • fooleap:看起来厚实,真香。
  • Sam.Z:Zabbix是开源的吧,用起如何,我们公...
  • huang:这是哪个页面的网址 http://www.sqrt...
  • huang:抱歉抱歉忘记了 http://www.sqrt4.cc...
  • huang:我把上传后生成压缩的图片的功能给禁...
  • 两个字:都一样的,你有虚拟lede软路由的话就...

日志存档

  • 2019年一月
  • 2018年十二月
  • 2018年十一月
  • 2018年十月
  • 2018年七月
  • 2018年六月
  • 2018年五月
  • 2018年四月
  • 2018年三月
  • 2018年一月
  • 2017年十月
  • 2017年九月
  • 2017年八月
  • 2017年七月
  • 2017年二月
  • 2017年一月
  • 2016年十二月
  • 2016年十一月
  • 2016年十月
  • 2016年七月
  • 2016年六月
  • 2016年四月
  • 2016年二月
  • 2016年一月
  • 2015年十二月
  • 2015年十月
  • 2015年九月
  • 2015年七月
  • 2015年五月
  • 2015年四月
  • 2015年三月
  • 2015年二月
  • 2015年一月
  • 2014年十二月
  • 2014年十月
  • 2014年九月
  • 2014年八月
  • 2014年七月
  • 2014年六月
  • 2014年五月
  • 2014年四月
  • 2014年三月
  • 2014年二月
  • 2014年一月
  • 2013年十二月
  • 2013年十一月
  • 2013年十月
  • 2013年九月
  • 2013年八月
  • 2013年七月
  • 2013年六月
  • 2013年五月
  • 2013年四月
  • 2013年三月
  • 2013年二月
  • 2013年一月
  • 2012年十二月
  • 2012年十一月
  • 2012年九月
  • 2012年八月
  • 2012年七月
  • 2012年六月
  • 2012年五月
  • 2012年四月
  • 2012年三月
  • 2012年二月
  • 2012年一月
  • 2011年十二月
  • 2011年十一月
  • 2011年十月
  • 2011年九月
  • 2011年八月
  • 2011年七月
  • 2011年六月
  • 2011年五月
  • 2011年四月
  • 2011年三月
  • 2011年二月
  • 2011年一月
  • 2010年十二月
  • 2010年十一月
  • 2010年十月
  • 2010年九月
  • 2010年八月
  • 2010年七月

W3C

  • XHTML 1.0 Transitional
  • CSS level 3
  • Google+
Copyright © 2010-2019 老谢博客 All rights reserved.
Gzipped 74.8% | Optimized loading 9 queries in 1.400 seconds | Memory 44.26 MB | 皖ICP备13010663号-1
Powered by WordPress. | Hosted By 阿里云 | Theme by WordPress主题巴士 | 站点地图 | SiteMap | Uptime |