首页 > 代码库 > oc面向对象初步
oc面向对象初步
//建立一个Car类.h文件#import <Foundation/Foundation.h>interface Car : NSObject{ @public int wheels;}- (instancetype)init;- (void)runWithSpeed :(int) speed AndDirection : (int) direction;+ (NSString *) fetchCarBrand;@end//建立一个Car类.m文件#import <Foundation/Foundation.h>#import <Car.h>@implementation Car+ (void)initialize{ [super initialize]; [self fetchCarBrand]; NSLog(@"%s", __PRETTY_FUNCTION__);}- (instancetype)init{ if ([super init]!=nil) { wheels = 90; brand = nil; } return self;}#import <Foundation/Foundation.h>#import "Car.h"@interface QQCar : Car{ int cheapPrice;}- (instancetype)initWithPrice: (int) price;@end//建立一个QQCar类.h文件#import "QQCar.h"@implementation QQCar- (instancetype)init{ if ([super init]) { cheapPrice = 30000; } return self;}- (instancetype)initWithPrice: (int) price{ if ([super init]) { cheapPrice = price; } return self;}@end//建立一个QQCar类.m文件#import <UIKit/UIKit.h>@interface ViewController : UIViewController@end//建立一个ViewController类.h文件#import "ViewController.h"#import "Car.h"#import "QQCar.h"@interface ViewController ()@end//建立一个ViewController类.m文件@implementation ViewController- (void)viewDidLoad{ [super viewDidLoad]; Car *car = [[Car alloc]init]; car->wheels = 4; [car runWithSpeed:100 andDirection:1]; [Car fetchCarBrand]; QQCar *qqcar = [[QQCar alloc] initWithPrice:40000]; [qqcar runWithSpeed:30 andDirection:2];}@end
oc面向对象初步
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。