首页 > 代码库 > 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面向对象初步