`
R任轩
  • 浏览: 14873 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

iOS self和super的区别

 
阅读更多

首先先写两个类 fist和two,two继承fist类

@interface First:NSObject
{
    int a;//声明了一个变量
}
-(void) setA:(int)c;//对a赋值的方法
@end
@implementation First
 
-(void)setA:(int)c
{
    a=c;
}
 
@end
 
//类2
@interface Two : First
{
    int b;
}
-(void)setB:(int)d;
-(void)print;
@end
@implementation Two
-(void)setB:(int)d
{
    b=d;
}
-(void)print
{
    NSLog(@"a的值为%d/nb的值为%d",a,b);
    NSLog(@"这时self==%@",[self class]);
    NSLog(@"super=====%@",[superclass]);
}
 
@end
 
 
int main(int argc, const char * argv[])
{
 
    @autoreleasepool {
            
        Two *two=[[Two alloc] init];
        NSLog(@"这时frist中的函数");
        [two setB:2];
        NSLog(@"这是two中的函数");
        [two setA:1];
        
        [two print];
        
      
    }
    return 0;
}

运行的结果是:

这时我们看到self和super的值一样  区别在哪呢????

 

其实是这样的  self和super都是指的是类的对象   self指的是本类的对象,而super指的是父类的对象

他们相当于系统自己声明的一个类对象。

 

分享到:
评论

相关推荐

    IOS self和super详解实现原理及区别

    主要介绍了iOS self和super详解实现原理及区别的相关资料,这里不仅说明区别并介绍实现原理,具有参考价值,需要的朋友可以参考下

    iOSputao#iOS-#[self class] 与 [super class]1

    /* For compatibility with old objc-runtime.h header *//* super_class is the firs

    ios-iOS左滑菜单栏.zip

    github:https://github.com/Super-lying/MenuDemo ...除了滑动展开和关闭菜单外,其他打开关闭方法: //展开菜单栏 [self.menu show]; //关闭菜单栏 [self.menu hidenWithAnimation];

    ios-RSTabBarControllers.zip

    CGPoint newP = [self convertPoint:point toView:self.centerButt]; if ( [self.centerButt pointInside:newP withEvent:event]) { return self.centerButt; }else{ return [super hitTest:point ...

    ios-平滑处理导航栏的显示和隐藏.zip

    super.viewDidLoad() self.navBarBgAlpha = 0 self.navBarTintColor = .white } func scrollViewDidScroll(_ scrollView: UIScrollView) { if scrollView.contentOffset.y > 100 { ...

    ios初级笔记

    if(self = [super init]){ _age = age; _no = no; } return self; } //重写父类的的description方法 //当使用%@带打印一个对象时候,会调用这个方法 - (NSString *)description{ NSString *str = ...

    ios-一句代码实现iOS检测更新软件.zip

    [super viewDidAppear:animated]; //一句代码实现检测更新 [self hsUpdateApp]; } 简书详细介绍地址: http://www.jianshu.com/p/1d08c786b52f github地址: https://github.com/wolfhous/HSUpdateApp

    ios-ios横向菜单 页面滑动.zip

    https://github.com/Super-lying/LyScrollMenu 横向菜单 页面滑动 例子 UIViewController *VC1 = [[UIViewController alloc]init]; [VC1.view setBackgroundColor:[UIColor redColor]]; ...

    IOS入门之HelloWorld源代码

    [super dealloc]; } - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen ...

    More iOS 6 Development Further Explorations of the iOS SDK

    Whether you're a self-taught iPhone and iPad apps development genius or have just made your way through the pages of Beginning iOS 6 Development, we have the perfect book for you. More iOS 6 ...

    ios开发记录

    //initWithNibName将控制器绑定xib的方法,如果xib的名称和控制器的类名称相同的时候,直接写init(会自动绑定同名的xib)就可以,如果xib的名称和类名称不同的话,必须手动调用此方法来进行绑定 ...

    ios归档代码

    self [super init]; if self nil { self comment [aDecoder decodeObjectForKey:@"comment"]; self content [aDecoder decodeObjectForKey:@"content"]; self creatTime [aDecoder ...

    3D动画 iOS

    super.viewDidLoad() // create a new scene let scene = SCNScene(named: "art.scnassets/ship.scn")! // create and add a camera to the scene let cameraNode = SCNNode() cameraNode.camera = ...

    SearchBar和tableView 组合并且不遮住状态栏

    ios7 demo下载 searchbar与状态栏重叠已修复。 关键代码: -(void)viewDidLoad{ [super viewDidLoad]; // self.table.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];//去除多余行 //设置table...

    ios-使用collectionView的无限轮播图.zip

    https://github.com/Super-lying/LyScrollBanner 简单使用方法 CGRect rect1 = CGRectMake(0, 300, 320, 220); NSArray *arrHttp = @[@"http://pic9.nipic.com/20100909/3398346_111317208068_2.jpg",@...

    ios实现UITableView之间圆角和间隙

    ios实现UITableView之间圆角和间隙效果,上图 实现UITableView 之间的圆角和间隙 废话不多说,直接上代码 第一步 去除系统默认tableview分割线 [self.homeView.tableOrder setSeparatorStyle:...

    iOS自定义圆形进度提示控件

    这个是绘制图形的类,绘制一个圆形的背景和扇形的进度。具体实现如下: import UIKit class ProgressControl: UIView { override init(frame: CGRect) { super.init(frame: frame) self.backgroundColo

    iOS 自定义状态栏和导航栏详细介绍

    iOS 自定义状态栏和导航栏  开发IOS APP 经常会根据需求更改状态栏和导航栏,这里整理了几种方法,大家可以看下。 导航栏透明 -(void)viewWillAppear:(BOOL)animated { //viewWillAppear中设置透明 [super ...

Global site tag (gtag.js) - Google Analytics