+表示类方法,-表示实例方法
总结一下几点:
1.类(static)方法
a. 类方法的调用
[类名称 类方法名称];
[Human toString];
注意:类方法
1,类方法可以调用类方法。
2,类方法不可以调用实例方法,但是类方法可以通过创建对象来访问实例方法。
3,类方法不可以使用实例变量。类方法可以使用self,因为self不是实例变量。
4,类方法作为消息,可以被发送到类或者对象里面去(实际上,就是可以通过类或者对象调用类方法的意思)。
2.实例方法
a.实例方法的调用
首先需要实例化该类
Human *man = [Human new]; 或者 Human *man = [Human alloc] init];
[类的实例 实例方法名称];
[man showSex];
注意:此处实例化该类时,调用了该类的构造函数init
并且该类的构造函数调用[super init]的返回值 不等于 该类的self。
定义子类的实例
Woman *wife = [Woman new];
此处实例化该类时,调用了该类的构造函数init
并且该类的构造函数调用[super init]的返回值 等于 该类的self。
3.OverWrite方法(覆写,重载)
覆写父类的+(void)toString
调用[Woman toString];
4.属性(property)
声明一个成员变量BOOL married;
声明set方法-(void) setMarried:(BOOL)m;
声明get方法-(BOOL) Married;
或者可以使用关键字@property
@synthesize
定义一个Human父类
定义部分:
[html] view plain copy print ?
- //
- // Human.h
- // OOP
- //
- // Created by jimmy.yang on 11-2-9.
- // Copyright 2011 __MyCompanyName__. All rights reserved.
- //
-
- #import <Foundation/Foundation.h>
-
-
- @interface Human : NSObject {
- BOOL sex;
- }
-
- +(void) toString;
-
- -(void) showSex;
-
- @end
//
// Human.h
// OOP
//
// Created by jimmy.yang on 11-2-9.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Human : NSObject {
BOOL sex;
}
+(void) toString;
-(void) showSex;
@end
注:+(void)前的加号,就表示这一个是类方法(static 方法),而-(void)表示这是一个实例方法
实现部分:
注意:下面的 -(id) init 即为构造函数.
[html] view plain copy print ?
- //
- // Human.m
- // OOP
- //
- // Created by jimmy.yang on 11-2-9.
- // Copyright 2011 __MyCompanyName__. All rights reserved.
- //
-
- #import "Human.h"
-
-
- @implementation Human
-
- //构造函数
- -(id) init
- {
- NSLog(@"init() in Human is called");
- sex = TRUE;
- return(self);
- }
-
- //static类方法
- + (void)toString
- {
- NSLog(@"this is a class method of Human");
- }
-
-
- //实例方法
- - (void)showSex
- {
- NSLog(@"my sex is %@",sex?@"MALE":@"FEMALE");
- }
-
-
- @end
//
// Human.m
// OOP
//
// Created by jimmy.yang on 11-2-9.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "Human.h"
@implementation Human
//构造函数
-(id) init
{
NSLog(@"init() in Human is called");
sex = TRUE;
return(self);
}
//static类方法
+ (void)toString
{
NSLog(@"this is a class method of Human");
}
//实例方法
- (void)showSex
{
NSLog(@"my sex is %@",sex?@"MALE":@"FEMALE");
}
@end
再来定义一个Woman子类
定义部分:
[html] view plain copy print ?
- //
- // Woman.h
- // OOP
- //
- // Created by jimmy.yang on 11-2-9.
- // Copyright 2011 __MyCompanyName__. All rights reserved.
- //
-
- #import <Foundation/Foundation.h>
- #import "Human.h"
-
-
- @interface Woman : Human {
- BOOL married;
- }
-
- -(void) canCook:(NSString*) foodName;
-
- -(void) setMarried:(BOOL)m;
-
- -(BOOL) Married;
-
- @end
//
// Woman.h
// OOP
//
// Created by jimmy.yang on 11-2-9.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Human.h"
@interface Woman : Human {
BOOL married;
}
-(void) canCook:(NSString*) foodName;
-(void) setMarried:(BOOL)m;
-(BOOL) Married;
@end
实现部分:
注意下面的:setMarried 与 Married 就是obj-C中属性的标准写法(当然以后还能看到其它简化的写法)
[html] view plain copy print ?
- //
- // Woman.m
- // OOP
- //
- // Created by jimmy.yang on 11-2-9.
- // Copyright 2011 __MyCompanyName__. All rights reserved.
- //
-
- #import "Woman.h"
-
-
- @implementation Woman
-
- //Woman类的构造函数
- -(id) init{
- NSLog(@"init() in Woman is called!");
- if (self==[super init]){
- sex = FALSE;
- married = FALSE;
- }
- return (self);
- }
-
- //overwrite父类中的toString()
- +(void)toString
- {
- NSLog(@"This is Woman's ToString()");
- }
-
- //Woman能做饭
- -(void)canCook:(NSString*) foodName
- {
- NSLog(@"I can cook %@",foodName);
- }
-
- //属性的setter
- -(void) setMarried:(BOOL)m
- {
- married = m;
- }
-
- //属性的getter
- -(BOOL) Married
- {
- return married;
- }
-
- @end
//
// Woman.m
// OOP
//
// Created by jimmy.yang on 11-2-9.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "Woman.h"
@implementation Woman
//Woman类的构造函数
-(id) init{
NSLog(@"init() in Woman is called!");
if (self==[super init]){
sex = FALSE;
married = FALSE;
}
return (self);
}
//overwrite父类中的toString()
+(void)toString
{
NSLog(@"This is Woman's ToString()");
}
//Woman能做饭
-(void)canCook:(NSString*) foodName
{
NSLog(@"I can cook %@",foodName);
}
//属性的setter
-(void) setMarried:(BOOL)m
{
married = m;
}
//属性的getter
-(BOOL) Married
{
return married;
}
@end
main方法中的调用:
[html] view plain copy print ?
- #import <Foundation/Foundation.h>
- #import "Human.h"
- #import "Woman.h"
-
- int main (int argc, const char * argv[]) {
- NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
-
- // insert code here...
- NSLog(@"Hello, World!");
-
- //调用类的“静态”方法
- [Human toString];
-
- NSLog(@"----------------");
-
- //创造一个Human的实例
- Human *man = [Human new];
-
- //调用man的showSex方法
- [man showSex];
-
- NSLog(@"----------------");
-
- //定义一个Woman子类的实例
- Woman *wife = [Woman new];
- [wife canCook:@"Rice"];
-
- //调用继承自父类的方法
- [wife showSex];
-
- //设置属性
- [wife setMarried:TRUE];
-
- //读取属性值
- NSLog(@"wife's married = %@",wife.Married==NO?@"FALSE":@"TRUE");
-
- NSLog(@"----------------");
-
- //调用overwrite后的toString方法
- [Woman toString];
-
-
- //Factory模式中常用的手法,在这里依然适用(只不过编译时会有警告 'Human' may not respond to '-canCook:')
- Human *wife2 = [Woman new];
- [wife2 canCook:@"soap"];
-
-
-
- NSLog(@"----------------");
-
- [pool drain];
- return 0;
- }
#import <Foundation/Foundation.h>
#import "Human.h"
#import "Woman.h"
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// insert code here...
NSLog(@"Hello, World!");
//调用类的“静态”方法
[Human toString];
NSLog(@"----------------");
//创造一个Human的实例
Human *man = [Human new];
//调用man的showSex方法
[man showSex];
NSLog(@"----------------");
//定义一个Woman子类的实例
Woman *wife = [Woman new];
[wife canCook:@"Rice"];
//调用继承自父类的方法
[wife showSex];
//设置属性
[wife setMarried:TRUE];
//读取属性值
NSLog(@"wife's married = %@",wife.Married==NO?@"FALSE":@"TRUE");
NSLog(@"----------------");
//调用overwrite后的toString方法
[Woman toString];
//Factory模式中常用的手法,在这里依然适用(只不过编译时会有警告 'Human' may not respond to '-canCook:')
Human *wife2 = [Woman new];
[wife2 canCook:@"soap"];
NSLog(@"----------------");
[pool drain];
return 0;
}
运行结果:
2011-02-09 17:01:02.016 OOP[1725:a0f] Hello, World!
2011-02-09 17:01:02.053 OOP[1725:a0f] this is a class method of Human
2011-02-09 17:01:02.062 OOP[1725:a0f] —————-
2011-02-09 17:01:02.075 OOP[1725:a0f] init() in Human is called
2011-02-09 17:01:02.091 OOP[1725:a0f] my sex is MALE
2011-02-09 17:01:02.094 OOP[1725:a0f] —————-
2011-02-09 17:01:02.099 OOP[1725:a0f] init() in Woman is called!
2011-02-09 17:01:02.104 OOP[1725:a0f] init() in Human is called
2011-02-09 17:01:02.105 OOP[1725:a0f] I can cook Rice
2011-02-09 17:01:02.108 OOP[1725:a0f] my sex is FEMALE
2011-02-09 17:01:02.109 OOP[1725:a0f] wife’s married = TRUE
2011-02-09 17:01:02.111 OOP[1725:a0f] —————-
2011-02-09 17:01:02.116 OOP[1725:a0f] This is Woman’s ToString()
2011-02-09 17:01:02.120 OOP[1725:a0f] init() in Woman is called!
2011-02-09 17:01:02.121 OOP[1725:a0f] init() in Human is called
2011-02-09 17:01:02.123 OOP[1725:a0f] I can cook soap
2011-02-09 17:01:02.125 OOP[1725:a0f] —————-