老谢博客

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

暂时没有评论!

发表评论 点击取消评论.

*必填

*必填

  • 文章归档
  • 子网计算
  • 我的共享
  • 锻炼计划
  • 给我留言
  • 关于老谢
2025 年 5 月
一 二 三 四 五 六 日
 1234
567891011
12131415161718
19202122232425
262728293031  
« 4 月    

最新文章

  • 认知,是否是一座大山?当架构决策变成配置清单比价
  • 重装博客服务器环境
  • 特斯拉24款标续 Model Y 2万公里使用体验
  • 接盘的傻子
  • 小牛us电瓶指示灯闪三次不上电
  • 一次还不错的小米售后体验
  • 装台1600元办公主机
  • 2021好久没更新博客
  • Zabbix监控oxidized备份状态
  • Zabbix 5.0 LTS版本MySQL表分区及编译安装随记

最新评论

  • 老陳网志:有点高端,像我们整点nas玩玩就够...
  • springwood:自从 CentOS 不维护之后,我换 U...
  • 大D:难都搞下来了,那就更得YM了
  • 大D:只能是YM了,谢总牛啊
  • 灰常记忆:经济不好 今年我也换了机器 一...
  • 大峰:这是海外服务器嘛?速度挺快的。
  • 大D:只能单走一个6了哈哈哈
  • zwwooooo:买特斯拉和买iPhone的人群其实相似...
  • 平安家属子痕:一直坚持油车,看你写的心里有...
  • 秦大叔:室内每年能开2万公里电车确实划算 ...

日志存档

  • 2025 年 5 月
  • 2025 年 4 月
  • 2025 年 3 月
  • 2024 年 9 月
  • 2024 年 5 月
  • 2024 年 1 月
  • 2023 年 4 月
  • 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-2025 老谢博客 All rights reserved.
Gzipped 76.5% | Optimized loading 45 queries in 0.565 seconds | Memory 39.18 MB | 尼玛的备案
Powered by WordPress. | Hosted By LAOXUEHOST | Theme by WordPress主题巴士 | 站点地图 | SiteMap | uptime查询