黑马程序员——OC基础---类方法的总结
来源:互联网 发布:动漫人物模型数据 编辑:程序博客网 时间:2024/04/30 19:30
------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------
1>>方法定义
+ (void) abc:(int) a andOther:(float)b;
其中‘+’为方法类型,不可省,表示这是一个类方法,区别于‘-’表示对象方法。
(void)为方法返回类型,不可省。
abc: andOther:为方法名,注意方法名包括冒号(冒号不可省),其中‘andOther’为对后一个参数的简要说明,可省(为了提高程序可读性,原则上不省略),‘andOther’与前一个参数之间必须有空格
(int)a 和 (float)b为方法的参数,如果方法不需要参数,则可省。
注意,1)每个冒号对应一个参数,如果没有参数,就不需要冒号,有一个参数,就需要一个冒号。
2)类方法名可以和对象方法名完全重名,通过‘+’‘-’来区别到底是类方法还是对象方法
-(void) abc:(int) a andOther:(float)b;
2>>方法的调用
类方法能并且只能被类调用 [类名 类方法名];
给对象发送类方法消息不会被执行,但是对象方法可以调用类方法(不限于本类,也可以是其他类的类方法),格式仍然是在对象方法中用类名调用类方法 [类名 类方法名];
类方法也同样可以调用对象方法(不限于本类,也可以是其他类),此时需要传入一个对象参数,然后用对象参数调用对象方法 [对象 对象方法名];
注意:在类方法中用self调用对象方法是错误的 [self 对象方法];
在对方法中用self调用类方法也是错误的 [self 类方法];
类方法不可以调用自身,会造成死循环。
Solider.h#import <Foundation/Foundation.h>@class Gun;@class Bullet;@interface Solider : NSObject@property(nonatomic,copy)NSString *name;@property(nonatomic,strong)Gun *gun;-(void)run;+(void)dun:(Solider*)solider;-(void)tiao;-(void)fff:(int )a :(float)b :(char)c;//对象方法可以与下面的类方法同名,唯独用‘+’‘-’来标记不同+(void)fff:(int )a :(float)b :(char)c;-(void)fire:(Solider *)soldier andGun:(Gun*) gun andBullet:(Bullet* )bullet;@end注意:-(void)fff:(int )a :(float)b :(char)c;与+(void)fff:(int )a :(float)b :(char)c;仅为验证可以同名定义,并未在Solider.m中实现
Solider.m#import "Solider.h"#import "Gun.h"@implementation Solider-(void)run{ NSLog(@"士兵在跑");}+(void)dun:(Solider*)solider{ [Gun shooot]; [solider tiao];//类方法中通过传入的对象名调用对象方法// [self run];//在类方法中用 self调用对象方法会报错}-(void)tiao{ NSLog(@"士兵跳了起来");}-(void)fire:(Solider *)soldier andGun:(Gun*) gun andBullet:(Bullet* )bullet{ [soldier run]; [Solider dun:soldier];//对象方法中用类名调用了类方法// [self dun];//对象方法中用 self调用类方法会报错 [gun shoot:bullet andSoldier:self];}@end
Gun.h#import <Foundation/Foundation.h>@class Bullet;@class Solider;@interface Gun : NSObject@property(nonatomic,copy)NSString *size;-(void)shoot:(Bullet *)bullet andSoldier:(Solider *) solider;+(void)shooot;@end
Gum.m#import "Gun.h"#import "Bullet.h"#import "Solider.h"@implementation Gun-(void)shoot:(Bullet *)bullet andSoldier:(Solider *) solider{ if ([bullet bulletCount]>0) { [bullet setBulletCount:[bullet bulletCount]-1]; NSLog(@"啪啪啪。。。%@用%@在射击。。。当前子弹剩余%d", [solider name],[self size],[bullet bulletCount]); }else { NSLog(@"没有子弹了,赶紧内裤挂枪投降吧!"); }}+(void)shooot{ NSLog(@"shooot");}@end
Bullet.h#import <Foundation/Foundation.h>@interface Bullet : NSObject@property(nonatomic,assign)int bulletCount;@end
Bullet.m#import "Bullet.h"@implementation Bullet@end
- 黑马程序员——OC基础---类方法的总结
- 黑马程序员——OC基础学习(二)---对象方法和类方法的学习知识总结
- 黑马程序员——OC基础语法----类、对象、方法
- 黑马程序员-oc基础-集合的总结
- 黑马程序员——OC基础---简单的OC程序
- 黑马程序员-oc基础类方法 封装
- 黑马程序员——OC类方法
- 黑马程序员---OC基础---OC简介、OC与C的差异、类和对象与方法
- 黑马程序员——OC基础——构造方法
- 黑马程序员——OC基础——构造方法
- 黑马程序员——OC---类的方法相关
- 黑马程序员—OC语言基础—方法小结
- 黑马程序员——OC 基础:setter和getter方法
- 黑马程序员——OC语言基础---对象和方法
- 黑马程序员——OC基础---构造方法
- 黑马程序员—OC基础-- ==和 isEqual方法
- 黑马程序员——OC基础---基础小结(类方法,匿名对象,封装)
- 黑马程序员—OC基础--基础数据类型和包装类
- Linux的五个重启命令
- zookeeper学习与应用
- 【NOIP2011】Mayan游戏 搜索
- bootstrap中fuelux.tree的简单使用
- iOS9适配
- 黑马程序员——OC基础---类方法的总结
- 从u盘创建乌班图启动盘并且安装乌班图
- CocoaPods安装使用及配置私有库以及管理依赖库
- stm8 time1精准延时
- ksh 控制键
- NAT(Network Address Translation,网络地址转换)
- 配置文件<lib>节点里没有libtest项;
- 通过hibernate API编写访问数据库的代码
- C#点击iframe页面的控件(button),弹出层,关闭弹出层时只刷新父页面中的iframe里面的页面