OC类的继承-super关键字
来源:互联网 发布:百度软件中心 知乎 编辑:程序博客网 时间:2024/05/17 01:14
//
// main.m
// super关键字
//
// Created by Goddog on 15/1/7.
// Copyright (c) 2015年 Goddog. All rights reserved.
//
/*
1.如果需要在子类方法中调用父类被覆盖的实例方法,可以使用super关键字来调用。
2.super同self一样不能出现在类方法中,类方法的调用者只能是类的本身。
3.子类继承父类时,子类可以获得父类中定义的成员变量,子类接口部分不能定义与父类接口部分重命名的成员变量。
4.在类实现部分定义的成员变量被限制在该类的内部,故子类完全可以与父类实现部分定义的成员变量同名。
5.当子类实现部分定义了与父类重名的成员变量,子类的成员变量就会隐藏父类的成员变量,可以调用父类的方法来访问父类中被隐藏的成员变量。
6. 虽然程序只创建一个对象,但是对象内部还是有两块内存来保存a的成员变量,一块父类隐藏的,一块子类实现的。
*/
//// Father.h// super关键字//// Created by Goddog on 15/1/7.// Copyright (c) 2015年 Goddog. All rights reserved.//#import <Foundation/Foundation.h>@interface Father : NSObject//定义成员变量{ int _a;}@property (nonatomic,assign) int a;-(void) see;@end
//// Father.m// super关键字//// Created by Goddog on 15/1/7.// Copyright (c) 2015年 Goddog. All rights reserved.//#import "Father.h"@implementation Father@synthesize a = _a;-(id) init{ if (self == [super init]) { self->_a = 8; } return self;}-(void) see{ NSLog(@"父亲看到了美女一号");}@end
//// Son.h// super关键字//// Created by Goddog on 15/1/7.// Copyright (c) 2015年 Goddog. All rights reserved.//#import <Foundation/Foundation.h>#import "Father.h"@interface Son : Father-(void) access;-(void) look;@end
//// Son.m// super关键字//// Created by Goddog on 15/1/7.// Copyright (c) 2015年 Goddog. All rights reserved.//#import "Son.h"@implementation Son{ //隐藏父类的成员变量 int _a;}//重写init方法-(id) init{ if (self == [super init]) { self->_a = 12; } return self;}//子类方法-(void) access{ //如果直接访问是当前类中的成员变量 NSLog(@"直接访问时子类中的成员变量:%d",_a); //访问父类的被隐藏的成员变量 NSLog(@"访问父类中被隐藏的成员变量:%d",super.a);}//重写方法-(void) see{ NSLog(@"儿子看到了美女二号!");}//调用父类的方法-(void) look{ [super see];}@end
#import <Foundation/Foundation.h>#import "Son.h"int main(int argc, const char * argv[]) { @autoreleasepool { //实例化对象 Son* son = [[Son alloc] init]; //调用父类的see方法 [son look]; //看访问的是父类还是本身 [son access]; } return 0;}
0 0
- OC类的继承-super关键字
- 类的继承及super关键字
- 4.1类的继承与super关键字
- 类的继承——方法的重载,super关键字
- oc继承self,super的作用
- OC--super关键字
- oc中关键字super
- java继承中super关键字的用法
- 关于python的继承,super关键字
- 继承之super关键字的使用
- 继承中的final/super关键字、Object类
- 继承(包括super关键字)
- 继承与super关键字
- JAVA第七节-继承,继承中方法重写,继承的初始化顺序,final关键字,super关键字,object类
- 继承中this关键字和super关键字的作用
- OC-继承,多态,self,super
- 【Objective-C】08-OC对象的super关键字
- java中的继承,继承中方法的重写,继承的初始化顺序,final关键字,super关键字
- 【c语言】实现Strcat函数
- 低功耗蓝牙4.0BLE编程-nrf51822开发(6)-Battery Service
- 第十三篇 一个安装、管理windows服务的桌面程序
- HDU 水题 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016
- 粒子系统 和 摄像机
- OC类的继承-super关键字
- linux 下sed使用
- (ZT)iOS 调用地图导航
- nyoj 399 整除个数
- 静态变量使用技巧
- UVA - 11292 - Dragon of Loowater
- 游戏项目之打怪
- 装饰者模式Decorator(成年程序猿必须懂得一种姿势)—咱们就侃一侃
- C++各种数据类型表示范围