首页 > 代码库 > oc面向对象初步
oc面向对象初步
//Animal .h文件#import <Foundation/Foundation.h>@interface Animal : NSObject{ @private int _feets; int _eyes; NSString *_corlor;}- (void)cry;- (instancetype)initWithNumber :(int)feets;- (void)cryWithDB : (int)size;+ (NSString *) LetItGo;- (void) setEyes:(int)eyes;- (int) getEyes;@end//Animal .m文件@implementation Animal- (void)cry{ NSLog(@"%@ :%s",@"crying",__FUNCTION__);}-(instancetype)initWithNumber :(int)feets{ if ([super init]) { _feets=feets; _corlor=@"ghost"; } return self;}- (void)cryWithDB : (int)size{ NSLog(@"crying with %d DB",size);}+ (NSString *) LetItGo{ return @"It Is So Beatiful!";}- (void) setEyes:(int)eyes{ _eyes=eyes;}- (int) getEyes{ return _eyes;}@end//Cat .h文件#import "Animal.h"@interface Cat : Animal- (void)cry1;- (void)cry3;@end//Cat .m文件#import "Cat.h"@implementation Cat- (void)cry1{ NSLog(@"%@ :%s",@"crying_",__FUNCTION__);}- (void)cry{ NSLog(@"%@ :%s",@"crying__",__FUNCTION__);}- (void)cry3{ NSLog(@"%@ :%s",@"crying______",__FUNCTION__);}@end//Tig .h文件#import "Cat.h"@interface Tiger : Cat-(void)cry2;@end//Tig .m文件#import "Tiger.h"@implementation Tiger-(void)cry2{ NSLog(@"%@ :%s",@"crying___",__FUNCTION__);}-(void)cry1{ NSLog(@"%@ :%s",@"crying____",__FUNCTION__);}-(void)cry{ NSLog(@"%@ :%s",@"crying_____",__FUNCTION__);}@end//Viewcontroller .h文件#import <UIKit/UIKit.h>@interface ViewController : UIViewController@end//Viewcontroller .m文件#import "ViewController.h"#import "Animal.h"#import "Cat.h"#import "Tiger.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; Animal *animal=[[Animal alloc]initWithNumber:4]; [animal cryWithDB : 50]; NSString *bbs=[Animal LetItGo]; NSLog(@"%@",bbs); [animal setEyes:2]; NSLog(@"%d",animal.getEyes); Cat *cat=[[Cat alloc]init]; Tiger *tiger=[[Tiger alloc]init]; [animal cry]; [cat cry]; [tiger cry]; [cat cry1]; [tiger cry1]; [tiger cry2]; [animal release]; [cat release]; [tiger release]; animal=[[Cat alloc]init]; [animal cry]; [animal release]; animal=[[Tiger alloc]init]; [animal cry]; cat=[[Tiger alloc]init]; [cat cry]; [animal release]; [cat release]; animal=[[Cat alloc]init]; [animal cry3];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
oc面向对象初步
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。