黑马程序员——OC学习总结6-8

来源:互联网 发布:DPP无法更新镜头数据 编辑:程序博客网 时间:2024/05/30 23:44

---------------- ASP.Net+Unity开发、.Net培训、期待与您交流!----------------


《OC6-self、自动释放、变量作用域》学习总结


1、OC中的self既可以用在动态方法中,也可以用在静态方法中。

2、self在动态方法中。
#import "Student.h"@implementation Student- (void)test1 {    }- (void)test2 {    //  self代表着调用test2方法的Student对象.    //  作用是调用Student对象的动态方法test2.    [self test1];}@end
3、self在静态方法中。
#import "Student.h"@implementation Student+ (void)test1 {    }+ (void)test2 {    //  self代表着Student这个类.    //  作用是调用了静态方法test2.    [Student test1];    //  作用是调用了静态方法test2.    [self test1];}@end
4、在动态方法中,self代表着对象。

5、在静态方法中,self代表着类。

6、self代表着当前方法的调用者。

《OC7-OC语法简单复习、new关键字》学习总结


1、没有存储任何内存地址的指针称为空指针。

2、下面两个都是空指针。
#import <Foundation/Foundation.h>#import "Student.h"int main(int argc, const char * argv[]){    @autoreleasepool {                Student *s1 = NULL;                Student *s2 = nil;            }    return 0;}
3、野指针不是空指针,是指向不可用内存的指针,非常危险。

4、利用野指针发送消息是很危险的,会报错。

5、利用空指针发送消息是发布出去的,不会造成任何影响,不会报错。

6、如果一个对象已经被回收了,就不要再去操作它,不要再尝试给它发消息。

7、野指针示例。
#import <Foundation/Foundation.h>#import "Student.h"int main(int argc, const char * argv[]){    @autoreleasepool {        //  内存中有个指针变量stu,指向了Student对象.        Student *stu = [[Student alloc] init];        //  调用这个Student对象的setAge方法.        [stu setAge:10];        //  Student对象接收到release会马上销毁,所占的内存会被回收.        [stu release];        //  调用这个Student对象的setAge方法.        //  Student对象已经销毁了,内存已经被回收,你仍去访问它,肯定是不合法的.        [stu setAge:10];            }    return 0;}
8、stu指向Student对象,可以正常调用Student对象的setAge方法。


9、Student对象接收到release会立即被销毁,所占内存会被回收,所以不可用。


10、不能再调用Student对象的setAge方法了,因为内存已经不属于你。

《OC8-@property和@synthesize》学习总结


1、@property关键字功能:让编译器自动生成一个与数据成员同名的方法声明。

2、@property关键字示例。
#import <Foundation/Foundation.h>@interface Student : NSObject {    int _age;}//  与下面等效.@property int age;- (int)age;- (void)setAge:(int)age;@end
3、@synthesize关键字功能:让编译器自动生成一个与数据成员同名的方法实现,与@property关键字配对使用。

4、@synthesize关键字示例。
#import "Student.h"@implementation Student//  与下面等效.@synthesize age;- (int)age {    return _age;}- (void)setAge:(int)age {    _age = age;}@end

---------------- ASP.Net+Unity开发、.Net培训、期待与您交流! ----------------


----------------详细请查看:www.itheima.com-----------------------------------------


0 0
原创粉丝点击