返回首页
当前位置: 主页 > 网络编程 > 其他实例教程 >

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

时间:2015-10-24 20:58来源:知行网www.zhixing123.cn 编辑:麦田守望者

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

 

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

People.m


#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;
}

  在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

  分别是返回值为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

  这里没什么好说的,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;
}

  在这里通过创建的实例对象名分别调用成员方法并传递实参给成员方法,并分别赋值给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

  然后在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

  在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;
}

  输出结果:


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;
}

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

------分隔线----------------------------
标签(Tag):面向对象
------分隔线----------------------------
推荐内容
猜你感兴趣