OC-初始化、实例方法、类方法、工厂方法
来源:互联网 发布:美国情景喜剧排行知乎 编辑:程序博客网 时间:2024/06/05 02:10
1.初始化
1.1 无参初始化 1.1.1init是从父类NSObject中继承的,所以不需要在.h文件中声明
SHInteger.h#import <Foundation/Foundation.h>@interface SHInteger : NSObject//NSObject是父类@property int integer;-(void)show;@end
SHInteger.m#import "SHInteger.h"@implementation SHInteger-(instancetype)init//无参初始化{ if (self = [super init]) { self.integer = 50; } return self;}-(void)show{ NSLog(@"%d", self.integer);}@end
main:
#import <Foundation/Foundation.h>#import "SHInteger.h"int main(int argc, const char * argv[]) { @autoreleasepool { int a = 10; SHInteger *i1 = [[SHInteger alloc] init]; [i1 show]; } return 0;}
1.2 带参初始化 1.2.1方法名的格式为:-(id)initWith...1.3 instancetype:实例(对象)的数据类型,只用在init的返回值1.4 id: 1.4.1万能指针(数据类型),类似于void* 1.4.2与void*的区别 id定义指针时不需要加* id类型的指针不能指向基本数据类型(包括结构体)的变量,只能指向OC类的对象
id a;//a是一个指针 int b = 10;//a = &b;//id类型的指针a不能指向整型变量 a = @"This is a string.";//id类型的指针a只能指向OC类的对象
#import <Foundation/Foundation.h>@interface SHInteger : NSObject@property int integer;-(id)initWithInteger:(int)integer;-(void)show;@end
#import "SHInteger.h"@implementation SHInteger-(instancetype)init{ if (self = [super init]) { self.integer = 100; } return self;}-(id)initWithInteger:(int)integer{ if (self = [super init]) { self.integer = integer; } return self;}-(void)show{ NSLog(@"%d", self.integer);}@end
1.5 self 1.5.1 self是一个指针 1.5.2 只能用在类中方法的函数体中 1.5.3 指向调用该方法的对象 1.5.4 可以在类中的一个方法函数体中调用类中其他方法,包括私有方法1.6 super 1.6.1 super也是一个指针 1.6.2 该指针指向该所在类的父类数据
2.实例方法
2.1 以减号开头的方法2.2 用对象来调用2.3 在实例方法中self指向调用该方法的对象
3.类方法
3.1 以加号开头的方法3.2 用类名来调用3.3 在类方法中不能用self
#import <Foundation/Foundation.h>@interface SHClassMethod : NSObject-(void)show;+(void)classMethod;@end
#import "SHClassMethod.h"@implementation SHClassMethod-(void)show{ NSLog(@"这是一个实例方法");}+(void)classMethod{ NSLog(@"这是一个类方法");}@end
SHClassMethod *cm = [[SHClassMethod alloc] init];[cm show];//实例方法//[cm classMethod];//类方法不能用对象调用[SHClassMethod classMethod];//类方法使用类名调用的
4.工厂方法
4.1 工厂方法是类方法的一种应用4.2 工厂方法用于生成对象4.3 无参工厂方法4.4 带参工厂方法
#import <Foundation/Foundation.h>@interface SHPoint : NSObject@property int x;@property int y;-(void)show;+(id)point;//工厂方法的方法名就是类名,但不要前缀@end
#import "SHPoint.h"@implementation SHPoint-(instancetype)init{ if (self = [super init]) { self.x = 10; self.y = 20; } return self;}-(void)show{ NSLog(@"(%d,%d)", self.x, self.y);}+(id)point{ SHPoint *p = [[SHPoint alloc] init]; return p;}@end
带参工厂.h:+(id)integerWithInteger:(int)integer;.m:-(id)initWithInteger:(int)integer{ if (self = [super init]) { self.integer = integer; } return self;}+(id)integerWithInteger:(int)integer{ SHInteger *i = [[SHInteger alloc] initWithInteger:integer]; return i;}
5.单例模式
5.1 是一种特殊的工厂方法5.2 特殊在只能生成一个对象
#import <Foundation/Foundation.h>@interface SHSingleton : NSObject+(id)sharedSingleton;@end
#import "SHSingleton.h"@implementation SHSingleton+(id)sharedSingleton{ static SHSingleton *single = nil; if (single == nil) { single = [[SHSingleton alloc] init]; } return single;}@end
#import <Foundation/Foundation.h>#import "SHSingleton.h"int main(int argc, const char * argv[]) { @autoreleasepool { SHSingleton *s1 = [SHSingleton sharedSingleton]; NSLog(@"%p", s1); SHSingleton *s2 = [SHSingleton sharedSingleton]; NSLog(@"%p", s2); } return 0;}
运行结果:
2016-08-25 20:02:34.031 day15_14[2196:203901] 0x100200330
2016-08-25 20:02:34.032 day15_14[2196:203901] 0x100200330
思考练习
1、模拟登陆程序,实现用户类
属性:账号、密码(int)
方法:初始化(带参、不带参)、工厂方法(带参、不带参)、show、判断账号密码是否正确
2、太阳类
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
解析
#import <Foundation/Foundation.h>@interface SHUser : NSObject@property int account;@property int password;-(id)initWithAccount:(int)account andPassword:(int)password;+(id)user;+(id)userWithAccount:(int)account andPassword:(int)passowrd;-(void)show;-(BOOL)isEuqalToUser:(int)account withPassowrd:(int)password;@end
#import "SHUser.h"@implementation SHUser-(instancetype)init{ if (self = [super init]) { self.account = 1000; self.password = 1234; } return self;}-(id)initWithAccount:(int)account andPassword:(int)password{ if (self = [super init]) { self.account = account; self.password = password; } return self;}+(id)user{ SHUser *u = [[SHUser alloc] init]; return u;}+(id)userWithAccount:(int)account andPassword:(int)passowrd{ SHUser *u = [[SHUser alloc] initWithAccount:account andPassword:passowrd]; return u;}-(void)show{ NSLog(@"账号:%d,密码:%d", self.account, self.password);}-(BOOL)isEuqalToUser:(int)account withPassowrd:(int)password{ return self.account == account && self.password ==password;}@end
太阳类:
#import <Foundation/Foundation.h>@interface SHSun : NSObject+(id)sharedSun;@end
#import "SHSun.h"@implementation SHSun+(id)sharedSun{ static SHSun *single = nil; if (single == nil) { single = [[SHSun alloc] init]; } return single;}@end
main函数:
#import <Foundation/Foundation.h>#import "SHUser.h"#import "SHSun.h"int main(int argc, const char * argv[]) { @autoreleasepool { int account; NSLog(@"请输入一个注册账号:"); scanf("%d", &account); int password; NSLog(@"请输入一个注册密码:"); scanf("%d", &password); SHUser *user1 = [SHUser userWithAccount:account andPassword:password]; NSLog(@"请输入登录账号:"); scanf("%d", &account); NSLog(@"请输入登录密码:"); scanf("%d", &password); if ([user1 isEuqalToUser:account withPassowrd:password] == YES) { NSLog(@"登录成功"); } else { NSLog(@"登录账号或密码错误"); } SHSun *sun1 = [SHSun sharedSun]; SHSun *sun2 = [SHSun sharedSun]; NSLog(@"%p,%p", sun1, sun2); } return 0;}
0 0
- OC-初始化、实例方法、类方法、工厂方法
- [Object-C]OC实例变量初始化方法
- [Object-C]OC实例变量初始化方法
- oc工厂模式方法
- Objective-C:初始化、实例方法、类方法、工厂方法、单例模式
- oc初始化方法
- OC基础---初始化方法
- OC中的类方法与实例方法
- OC中的类方法与实例方法
- oc类初始化方法load和initialize
- OC 类的创建及初始化方法
- 工厂方法模式实例
- iOS-OC中的静态方法(类方法)与实例方法
- 静态工厂方法和实例工厂方法
- 静态工厂方法和实例工厂方法
- OC中的对象初始化方法
- OC之继承,初始化方法
- OC之继承、初始化方法
- 菜单点击弹出动画 ViewpropertyAnimator
- C#高级编程-事务和网络
- Android屏幕适配 超级解决方案percent-support-lib
- ELK日志管理系统的搭建
- 1015 德才论
- OC-初始化、实例方法、类方法、工厂方法
- bzoj 3337: ORZJRY I (块状链表)
- faster rcnn RPN之anchor(generate_anchors)源码解析
- POJ 3159 Candies(差分规划+SPFA)
- TCP slow start (TCP慢启动)
- Android图片加载库Glide
- sizeof 可变参数的模版特化 C++ templa
- 1016 部分a+b
- tjut 1247