[Objective-C] id、self、super
来源:互联网 发布:华硕驱动更新软件 编辑:程序博客网 时间:2024/06/05 20:46
id 数据类型可存储任何类型的对象。从某种意义说,它是一般对象类型。
例如 id number;
将number声明为id类型的变量。
可声明方法使其具有id类型的返回值,如下:
-(id) newObject: (int) type;
事实# id类型是通用指针类型
因为通过指针(也就是内存地址)引用对象,所以可以自由地将它们在id变量之间来回赋值,因此,返回id类型值的方法就是返回指向内存中某对象的指针。然后可以将该值赋给任何对象变量。
self 是什么 ,super 是什么
> 在动态方法中,self代表着"对象"
> 在静态方法中,self代表着"类"
> 万变不离其宗,记住一句话就行了:self代表着当前方法的调用者
self 和 super 是oc 提供的 两个保留字。 但有根本区别,
当使用self调用方法时,会从当前类的方法列表中开始寻找,如果没有,就从父类中再找,而使用super时,则从父类的方法列表中开始找,然后调用父类的这个方法给出栗子简单的用下self 和super 这两个保留字:
接口部分:
@interface PersonSelf : NSObject
{ //声明两个实例变量 int age; NSString * name; Student *s;}//定义方法-(NSString *) getName;-(void) setName:(NSString *)_name;-(int) getAge;-(void)setAge:(int)_age;@end实现部分
#import "PersonSelf.h"
#import "Student.h" //#import一般使用在实现文件中@implementation PersonSelf-(id)init{ if(self=[super init]){ //super可以调用父类中的方法 s=[[Student alloc]init]; NSLog(@"student:%@\n",s); } return self;}-(NSString *) getName{ return name;}-(void) setName:(NSString *)_name{ name=_name; [self setAge:14];//self可以去调用本类对象中的方法 NSLog(@"setAge"); NSLog(@"self class is: %@\n",[self class]); NSLog(@"super class is: %@\n",[super superclass]); }-(int) getAge{ return age;}-(void)setAge:(int)_age{ age=_age;}@end
详情请参考:
0 0
- [Objective-C] id、self、super
- Objective-C—self 、super、点语法、成员变量的作用域、@property和@synthesize、id
- Objective-C 的 self 和 super 详解
- Objective-C 的 self 和 super 详解
- Objective-C 中self 和 super
- Objective-C中的self和super
- Objective-C 中的关键字 self 和 super
- Objective-C 的 self 和 super 详解
- Objective-C 的 self 和 super 详解
- Objective-C 的 self 和 super 详解
- Objective-C 的 self 和 super 详解
- Objective-C的self和super详解
- Objective-C 的 self 和 super 详解
- Objective-C 的 self 和 super 详解
- Objective-C 的 self 和 super 详解
- Objective-c中的self和super
- Objective-C 的 self 和 super 详解
- Objective-C 的 self 和 super 详解
- Introduction
- 读书笔记1、谁说菜鸟不会数据分析(入门篇)
- LTE
- HDU 3746 Cyclic Nacklace (KMP 循环节)
- SpringMVC中怎样从一个Controller方法跳转到另一个方法,注意是跳转,不是调用如题类
- [Objective-C] id、self、super
- hdu 4027 Can you answer these queries?
- 正则表达式-笔记
- fedora10 安装tftp服务
- Python之学习笔记(常用内建模块)
- LeetCode Course Schedule
- Android中Activity启动模式详解,可以控制程序按home键后进来还会调用一个自己不喜欢的界面
- hdu 1004 Let the Balloon Rise
- utf8 cat正常vi乱码