1.OC的方法:声明
- (返回值) 方法名;//使用对象去调用 实例方法
+(返回值) 方法名;//使用类调用 类方法
2.继承:super关键字的作用只有一个:用于子类中调用父类的方法
3.特殊方法:初始化方法
Rich2nd.h #import <Foundation/Foundation.h> #import "RichMan.h" @interface Rich2nd : RichMan//确定继承关系 //继承特性:子类会继承父类多有的 实例变量 和 方法 // 子类可以重写父类的方法 // 可以再子类重写的方法中,调用父类的方法 - (void) dragRacing;//飙车 @end Rich2nd.m #import "Rich2nd.h" @implementation Rich2nd - (void) charge{ NSLog(@"刷老爸的卡"); [super charge];//调用父类的方法刷卡 super关键字的作用只有一个:用于子类中调用父类的方法 } //对父类的方法进行重写 子类的方法优先级最高 - (void) sayHi{ NSLog(@"我叫%@,能交个朋友么",_name); } - (void) dragRacing{ NSLog(@"飙车"); } @end main.m #import <Foundation/Foundation.h> #import "RichMan.h" #import "Rich2nd.h" int main(int argc, const char * argv[]) { @autoreleasepool { RichMan * richman = [[RichMan alloc] init]; [richman sayHi]; [richman charge]; // NSLog(@"富人%@",richman -> _name); Rich2nd * rich2nd = [[Rich2nd alloc] init]; rich2nd -> _name = @"萌小小艺"; [rich2nd sayHi]; [rich2nd charge]; [rich2nd dragRacing]; RichMan * richman1 = [[RichMan alloc]initWithName:@"深情君" WithSex:@"男" WithAge:80 WithMoney:10.0]; NSLog(@"%@,%@,%d,%.2f",richman1->_name,richman1->_sex,richman1->_age,richman1->_money); } return 0; }
作业:
打僵尸。需求:
定义普通僵尸类:实例变量:僵尸种类、僵尸总血量、僵尸每次失血量。方法:初始化、被打击失血、死亡。
定义路障僵尸类:实例变量:僵尸种类、僵尸总血量、僵尸每次失血量,道具,弱点。方法:初始化、被打击失血、失去装备、死亡。
定义铁桶僵尸类:实例变量:僵尸种类、僵尸总血量、僵尸每次失血量,道具,弱点。方法:初始化、被打击失血、失去装备、死亡。
For循环 模拟攻击,创建3个僵尸对象。
(1)、普通僵尸。总血量50,每次失血3.
(2)、路障僵尸。总血量80,有路障时,每次失血2,血量剩余一半时,失去路障,每次失血3.
(3)、铁桶僵尸。总血量120,有铁桶时,每次失血1,血量剩余1/3时,失去铁桶,每次失血3.
循环攻击过程中:每个僵尸被攻击时,输出本次丢失血量,剩余血量。丢失道具时,输出丢失的道具。僵尸死亡时,输出已死亡。
最后一个僵尸死亡时,攻击停止,循环结束。输出总攻击次数。