Objective-C语法之类和对象

网友投稿 622 2022-11-08

Objective-C语法之类和对象

Objective-C语法之类和对象

1、类和方法

类的实体变量的访问权限:

对应的代码

[cpp]  ​​view plain​​​ ​​​copy​​

@interface Worker : NSObject{char *name;@privateint age;char *evaluation;@protectedid job;float wage;@publicid boss;}

没有写关键字的实体变量,比如 char *name是@protected的。也就是说,默认是protected的。

2、创建类

1.1、新建版项目,按Command + N 新建文件,创建类Student ,继承与NSObject

1.2、生成student.h  和student.m

[cpp]  ​​view plain​​​ ​​​copy​​

#import @interface Student : NSObject@end

[cpp]  ​​view plain​​​ ​​​copy​​

#import "Student.h"@implementation Student@end

1.3、在头文件里添加类成员变量和方法

@interface

[cpp]  ​​view plain​​​ ​​​copy​​

#import @interface Student : NSObject{NSString *studentName;NSInteger age;}-(void) printInfo;-(void) setStudentName: (NSString*) name;-(void) setAge: (NSInteger) age;-(NSString*) studentName;-(NSInteger) age;@end

@interface 类的开始的标识符号 ,好比Java  或 C 语言中的Class@end 类的结束符号继承类的方式:Class:Parent,如上代码Student:NSObject成员变量在@interface Class: Parent { .... }之间成员变量默认的访问权限是protected。类成员方法在成员变量后面,格式是:: scope (returnType) methodName: (parameter1Type) parameter1Name;scope指得是类方法或实例化方法。类方法用+号开始,实例化方法用 -号开始。

1.4、实现类中的方法

@implementation

[cpp]  ​​view plain​​​ ​​​copy​​

#import "Student.h"@implementation Student-(void) printInfo{"姓名:%@ 年龄:%d岁",studentName,studentAge);}-(void) setStudentName: (NSString*) name{studentName = name;}-(void) setAge: (NSInteger) age{studentAge = age;}-(NSString*) studentName{return studentName;}-(NSInteger) age{return studentAge;}@end

1.5、创建类对象,调用方法。

[cpp]  ​​view plain​​​ ​​​copy​​

Student *student = [[Student alloc]init];[student setStudentName:@"张三"];[student setAge:10];[student printInfo];[student release];

Sutdent *student = [[Sutdent alloc] init]; 这行代码含有几个重要含义[Student alloc]调用Student的类方法,这类似于分配内存,[object init]是构成函数调用,初始类对象的成员变量。

打印结果:

[plain]  ​​view plain​​​ ​​​copy​​

姓名:张三 年龄:10岁

2、类的实例方法使用多个参数

2.1添加一个多参数的方法和实现

[cpp]  ​​view plain​​​ ​​​copy​​

....-(void) setNameAndAge:(NSString*) name setAge:(NSInteger) age;....

[cpp]  ​​view plain​​​ ​​​copy​​

....-(void) setNameAndAge:(NSString*) name setAge:(NSInteger) age{studentName = name;studentAge = age;}....

2.2调用

[cpp]  ​​view plain​​​ ​​​copy​​

Student *student = [[Student alloc]init];[student setNameAndAge:@"李四" setAge:20];[student printInfo];[student release];

打印结果:

[plain]  ​​view plain​​​ ​​​copy​​

姓名:李四 年龄:20岁

3、自定义构造函数

[cpp]  ​​view plain​​​ ​​​copy​​

....-(Student*) initWithNameAndAge:(NSString*) name setAge:(NSInteger) age;....

[cpp]  ​​view plain​​​ ​​​copy​​

....-(Student*) initWithNameAndAge:(NSString*) name setAge:(NSInteger) age{self = [super init];if ( self ) {[self setNameAndAge:name setAge:age];}return self;}....

-(id)init 这个方法用于类的初始化创建,每一个类在创建的时候需要调用init方法,使用父类的init 方法得到了self,这就可以做一些子类初始化的工作

3.2使用自定义构造函数:

[cpp]  ​​view plain​​​ ​​​copy​​

Student *student = [[Student alloc]initWithNameAndAge:@"rongfzh" setAge:6];[student printInfo];[student release];[student release];

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:实现滑动菜单SlidingMenu
下一篇:PHP中$_SERVER的用法
相关文章

 发表评论

暂时没有评论,来抢沙发吧~