黑马程序员——Objective-C——点语法、成员变量作用域、@property和@synthesize、id指针、构造方法...

-----Java培训、Android培训、iOS培训、.Net培训、期待与您交流!-----

一、点语法

1>OC中的点语法是替换set方法和get方法。

Student *stu=[Student new];
[stu setAge:100];
int age=[stu age];
//点语法替换后上面代码后的内容为:
Student *stu=[Student new];
stu.age=100;
int age=stu.age;
int age=[stu age];

2>点语法的本质

  点语法的本质是方法的调用,只不过是在编译过程中编译器会自动转换为set方法和get方法。

3>注意点

- (void) setAge:(int)age
{
    self.age=age;//产生死循环
}
-(int)age
{
return self.age;//产生死循环
}

  

二、成员变量的作用域

  1>基本概念

    局部变量、全局变量都有自己的作用域,成员变量也不例外。

  2>类型

    @private:只能在当前类的实现@implementation中直接访问。

    @protected:可以在当前类以及子类的实现@implementation中直接访问。

    @public:任何地方都可以访问。

    @package:同一个"框架"内可以访问,介于@private和@public之间。

  3>注意点

    没有@interface,只有@implementation,也可以开发一个类,但在其中的成员变量作用域是@private。

  4>实例代码

    

#import <Foundation/Foundation.h>
//-------------person声明---------//
@interface person:NSObject
{
    @public
    int _age;
    @private
    int _height;
    @protected
    int _weight;
}
- (void) setAge:(int)age;
- (int)age;

- (void)setHeight:(int)height;
- (void)height;

- (void)setWeight:(int)weight;
- (int)weight;

@end
//---------person实现-----------//
@implementation person
//年龄setter
- (void) setAge:(int)age
{
   _age=age;
}
//年龄getter
- (int)age
{
   return _age;
}
//身高setter
- (void)setHeight:(int)height
{
    _height=height;
}
//身高getter
- (void)height
{
    return _height;
}
//体重getter
- (void)setWeight:(int)weight
{
    _weight=weight;
}
//体重setter
- (int)weight
{
    return _weight;
}   
@end

//-------------student声明---------//
@interface student:person
{
  
}
- (void) run;

@end
//---------student实现-----------//
@implementation student

- (void)run
{
    NSLog(@"年龄%i岁的学生在跑步",_age);//可以访问_age变量
    //NSLog(@"身高为%d的学生在跑步",_height);//不能访问_height变量
    NSLog(@"体重为%d的学生在跑步",_weight);//可以访问_weight变量
}
   
@end

int main()
{
    student *s=[stuent new];
    [s run];
}

三、@property和@synthesize

  1>@property

    用在@interface中,用来自动生成setter和getter的声明

    用@property int age; 就能替换为下面两行代码:

- (void)setAge:(int)age;
- (int)age;

  2>@synthesize

    用在@implementation中,用来自动生成setter和getter的实现。

    手动实现:

      1.若手动实现setter,编译器就只会生成getter;

      2.若手动实现getter,编译器就只会生成setter;

      3.若同时生getter和setter编译器就不会自动生成不存在的变量;

    用@synthesize age;//(@synthesize age=age 或 @synthesize age=_age) 就能替换为下面的代码:

- (void)setAge:(int)age
{
   _age=age;
}
- (int)age
{
   return _age;
}

  3>在xcode 4.x以后就不用再写@synthesize。

   只需要在@interface中写:@property int age;就能生成成员变量、getter和setter方法的声明、getter和setter方法的实现,(缺点:作用域是private); 生成的变量名是下划线开头{_age}。

四、id万能指针

  1>作用:能指向、操作任何oc对象。

  2>示例代码

#import <Foundation/Foundation.h>

@interface person:NSObject
//age属性
@property int age;

@end

@implementation person
 
@end

//程序入口
int main
{
     //person对象
person *p=[person new]; [p setAge:10]; NSLog(@"%d",[p age]); //id指针 id pp=[person new]; [pp setAge:100]; NSLog(@"%d",[pp age]); }

  

五、构造方法

  在初始化对象的时候操作对象成员、赋值等操作。

  1>重写构造方法

#import <Foundation/Foundation.h>

@interface person:NSObject
{
    int _age;
}
@end
@implementation person
- (id)init
{
    if(self==[super init])
    { 
        //成员变量赋值等操作
        _age=10;
        NSLog(@"重写init赋值age成功");
    }
}
@end 
int main()
{
/* 完整的创建一个对象需要2个步骤
1.分配存储空间 +alloc
2.初始化 -init
*/
person *p=[person alloc];
[p init];
}

  2>自定义构造方法

#import <Foundation/Foundation.h>

@interface person:NSObject
{
    int _age;
}

@end
@implementation person
- (id)initAge:age
{
    if(self==[super init])
    { 
        //成员变量赋值等操作
        _age=age;
        NSLog(@"initAge赋值age成功");
    }
}
@end 
int main()
{ 
    /* 完整的创建一个对象需要2个步骤
      1.分配存储空间 +alloc 
      2.初始化 -init 
    */ 
    person *p=[[person alloc] initAge:100]; 
    NSLog(@"%d",p->_age); 
return 0; }

  

转载于:https://www.cnblogs.com/comcn/p/4351003.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值