变量,构造方法

变量,构造方法

一、property属性

在类中定义成员变量时,使用

1 @interface Person: NSObject{
2 
3     NSString *name;
4 
5 }
6 @end

时,需自己定义getter和setter方法,比较麻烦,而使用property时,系统自动定义了getter和setter方法,比较简便

 1 #import <Foundation/Foundation.h>
 2 #import "Car.h"
 3 
 4 @interface Person : NSObject
 5 
 6 @property (nonatomic, strong) NSString *name;
 7 @property (nonatomic, strong) Car *che;
 8 
 9 - (void)showCar;
10 
11 @end
 1 #import <Foundation/Foundation.h>
 2 
 3 typedef enum : NSUInteger {
 4     kCarTypeA3,//0
 5     kCarTypeA4,//1
 6     kCarTypeA5//2
 7 } kCarType;
 8 
 9 @interface Car : NSObject
10 
11 @property (nonatomic, strong) NSString *color;
12 @property (nonatomic, strong) NSString *brand;
13 @property (nonatomic, assign) kCarType type;
14 
15 @end

 

在主函数中调用:

 1 #import <Foundation/Foundation.h>
 2 #import "Car.h"
 3 #import "Person.h"
 4 
 5 int main(int argc, const char * argv[]) {
 6     @autoreleasepool {
 7         Car *cc = [Car new];
 8         [cc setColor:@"褐色"];
 9         cc.brand = @"大众";
10         cc.type = kCarTypeA3;
11         
12         Person *pp = [Person new];
13         pp.name = @"jack";
14         pp.che = cc;
15         
16         [pp showCar];
17         
18     }
19     return 0;
20 }

总结以下几点:

1.set方法:[cc setColor:@"red"]; 或者cc.color = @"red";

2.get方法:[cc name];或者cc.name;

3.点语法只能用于property属性的变量

 

二、构造方法

1.概念:自定义初始化方法 init方法,在创建这个对象的同时 对这个对象进行初始化,initWith开头,通常有3种返回类型 

- (id)

- (Car *)

- (instancetype)

2.用法举例

 1 #import <Foundation/Foundation.h>
 2 
 3 typedef enum : NSUInteger {
 4     kCarTypeA3,//0
 5     kCarTypeA4,//1
 6     kCarTypeA5//2
 7 } kCarType;
 8 
 9 @interface Car : NSObject
10 
11 @property (nonatomic, strong) NSString *color;
12 @property (nonatomic, strong) NSString *brand;
13 @property (nonatomic, assign) kCarType type;
14 
15 - (instancetype)initWithColor:(NSString *)aColor
16                      andBrand:(NSString *)aBrand
17                       andType:(kCarType)aType;
18 
19 @end
 1 #import "Car.h"
 2 
 3 @implementation Car
 4 
 5 //重写父类的init方法
 6 - (instancetype)init{
 7     //nil空 NULL指针为空
 8     //super 告诉编译器从我的上一级去查找(父类)
 9     //self 如果初始化失败 返回nil
10     self = [super init];
11     
12     if (self != nil) {
13         self.color = @"黑色";
14         self.brand = @"大众";
15         self.type = kCarTypeA4;
16     }
17     
18     return self;
19 }
20 
21 - (instancetype)initWithColor:(NSString *)aColor
22                      andBrand:(NSString *)aBrand
23                       andType:(kCarType)aType{
24     self = [super init];
25     
26     if(self != nil){
27         self.color = aColor;
28         self.brand = aBrand;
29         self.type = aType;
30     }
31     return self;
32 }
33 @end

3.注意:

1⃣️self = [super init];是对对象进行初始化,如果失败,则返回nil代表没有内存,成功则返回对象,NSString *str = nil; str没有内存,不存在

2⃣️一般自己重写构造方法,即用initWith,可随意改变初始值

3️⃣ 用init时,实例化可以用[[Person alloc] init];也可以用[Person new]; 用initWith时,只能用[[Person alloc] initWith];

posted @ 2018-07-26 23:54 健泽 阅读( ...) 评论( ...) 编辑 收藏
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值