OC-对象方法/类方法/self/super(1)
来源:互联网 发布:java 代码保护 编辑:程序博客网 时间:2024/06/06 00:28
1.在Objective-C中,对象方法类比于Java的静态方法和对象方法.代码如下
-声明
2014-06-05 10:18:50#import <Foundation/Foundation.h>@interface Student : NSObject//每个学生拥有学习的行为+ (void) study;// spendMoney: 称为方法名//(int)price 称为参数//(void)称为返回参数,必须用括号括起来- (void) spendMoney:(int)price;@end
-定义
#import "Student.h"@implementation Student+ (void) study{ NSLog(@"All student can study!");}- (void) spendMoney:(int)price{ NSLog(@"student spend %d yuan!",price);}@end
-调用
//类方法调用[Student study];//匿名对象方法的调用[[[Student alloc] init] spendMonery:20];
2.self/super:作为特殊的内置指针,谁调用指向谁(类/对象)
Student.h
#import <Foundation/Foundation.h>@interface Student : NSObject{ @public int _price;}//每个学生拥有学习的行为+ (void) study;// spendMoney: 称为方法名//(int)price 称为参数//(void)称为返回参数,必须用括号括起来- (void) spendMoney:(int)price;@end
Student.m
#import "Student.h"@implementation Student+ (void) study{ NSLog(@"All student can study!");}- (void) spendMoney:(int)price{ //同样,类调用时self指向的是类,对象调用时self指向的是对象, //self同时可以指向类和对象的属性 也可以作为方法的调用的指针 //当指向属性时,无论是其本身还是父类的属性. //因为在内存加载的时候首先会把对象的所有属性加载进来 self->_price=price; NSLog(@"student spend %d yuan!",self->_price);}@end
Academican.h
#import <Foundation/Foundation.h>#import "Student.h"@interface Academician : Student@end
#import "Academician.h"@implementation Academician+ (void) study{ //1.当外部调用为对象时 super指向的是对象,当外部调用为类时 super指向的是类 //2.super只能用来调用方法 不能用来作为指向属性的指针即super->_price 是 错误的 [super study]; NSLog(@"All Academician can study!");}- (void) spendMoney:(int)price{ NSLog(@"Academician spend %d yuan!",price);}@end
0 0
- OC-对象方法/类方法/self/super(1)
- 类、对象、方法、super、self、init方法
- 黑马程序员——入学Blog03----OC的self,super,类方法与对象方法
- 黑马视频学习笔记-OC-self,类方法,对象方法
- OC中类的Self 和 Super调用方法的本质
- OC基础day4-继承,self,super,多态,类对象,SEL类型,description,私有方法与私有变量,三种访问限制
- 黑马程序员--OC基础--类方法、对象方法和self的使用
- self 在OC中的作用(主要是对象方法和类方法中的作用)
- OC第三天-继承,初始化方法,便利构造器 ,self,super,方法重写
- OC类方法关于self的说明
- OC语言的第四天 static、self、super、继承、NSObject类、私有属性和私有方法、多态
- OC学习日记-继承,多态,私有属性、变量和方法,self,super,属性修饰符,description
- oc学习笔记(五)面向对象-类方法,self,NSString类
- 【OC易错】类方法中的self指的是类而不是对象
- OC视频笔记2.3(OC继承的使用)2.4(self与super)2.5(方法的重写)
- 黑马程序猿_OC之类方法与对象方法、self与super
- 【Objective-C】OC中对象创建,self,super的详解
- self,super 实例方法的一点理解
- delete指针之后应该赋值NULL
- Python为什么要self
- Java视频扩展知识 线程池的了解
- 利用Proxy Cache使Nginx对静态资源进行缓存
- IOS 实现录音PCM转MP3格式(边录音边转码)
- OC-对象方法/类方法/self/super(1)
- 修改eclipse中的默认maven2资源仓库保存地址
- ODBC和OLEDB的连接字符串
- 深入理解Java闭包概念
- 6月中国最美的地方!对的时间就要去对的地方!
- 解决PHP中文乱码问题 linux下页面乱码
- 深入理解Java内存模型(二)——重排序
- 编译安装PHP 时遇到问题解决方法.
- 爱加密亮相第十八届软博会,移动App安全引关注