码迷,mamicode.com
首页 > 其他好文 > 详细

1.4为类添加功能 :类方法和实例方法

时间:2014-10-19 12:57:03      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   ar   使用   for   

添加通过方法为类添加功能                                                              ios7cook目录

 

方法是类的建筑块。例如,一个名为Person类可以有,如散步逻辑功能,呼吸,吃饭,喝水。这些功能通常是封装的方法。
一种方法可以带参数,当调用方法,或者仅显示该方法时,调用者传递的变量。例如,在一个简单的世界里,我们有一个Person类的walk方法。如果你愿意,你可以添加一个参数或参数的方法,并将其命名为CGFloat类型的walkingSpeed??,所以,当另一个程序员调用你的类的方法,她可以指定哪些人有走的速度。你作为这个类的程序员,会再编写相应的代码,你的类来处理不同的速度行走。如果这一切听起来像太多的不要担心,但是看看下面的例子,在这里我添加了一个方法,我们在“创建并利用类”第5页Person类中创建的实现文件:
    

 #import "Person.h"
    @implementation Person
- (void) walkAtKilometersPerHour:(CGFloat)paramSpeedKilometersPerHour{ /* Write the code for this method here */
}
- (void) runAt10KilometersPerHour{
        /* Call the walk method in our own class and pass the value of 10 */
?8 |
Chapter 1: Implementing Controllers and Views
        [self walkAtKilometersPerHour:10.0f];
    }
@end

 


一个典型的方法在Objective-C以修饰符:
1,前缀告诉编译器的方法是否是一个实例方法( - )或类方法(+)。一个实例方法只能程序员分配和初始化你的类的实例,才能访问他。类方法可以直接从类本身调用它来访问。不要担心,如果这一切听起来很复杂。我们将看到在这本书中这些方法的例子,所以不要挂在这了吧。
对于方法2,一种数据类型,如果该方法返回的任何值。在我们的例子中,我们指定无效的,告诉我们编译器不返回任何东西。
3,方法名后面的第一个参数的第一部分。你没有返回任何参数的方法。你可以有不带参数的方法。
4,第一个参数如下后续的参数列表: 
让我告诉你一个方法的例子有两个参数:

- (void) singSong:(NSData *)paramSongData loudly:(BOOL)paramLoudly{ /* The parameters that we can access here in this method are:
         paramSongData (to access the song‘s data)
         paramLoudly will tell us if we have to sing the song loudly or not
         */
}

 


    
重要的是要记住,在每一个方法的每个参数都有一个外部和内部名称。外部名是该方法的一部分,而内部部分是实际的名称或可以在方法的实施中所用的参数的别名。在前面的例子中,第一个参数的外部名称是singSong,而它的内部名称是paramSongData。第二个参数的外部名称是loudly,但它的内部名称是paramLoudly。方法名和它的参数的外部名相结合,形成了被称为选择器的方法。的选择器,用于在这种情况下,上述方法将singSong:loudly:。一个选择,你会在本书后面看到的,是每一个方法的运行时标识符。没有两个里面一个类的方法可以有相同的选择。 

在我们的例子中,我们定义了三种方法为我们的Person类,其内部实施文件(Person.m):
•walkAtKilometersPerHour:

•runAt10KilometersPerHour

•  singSong:loudly:

如果我们希望能够使用从外界任何这些方法 - 例如,
从应用程序委托,我们应该暴露在我们的接口文件的方法(Person.h):

#import <Foundation/Foundation.h>
@interface Person : NSObject
@property (nonatomic, copy) NSString *firstName;
@property (nonatomic, copy) NSString *lastName;
- (void) walkAtKilometersPerHour:(CGFloat)paramSpeedKilometersPerHour;
- (void) runAt10KilometersPerHour;
/* Do not expose the singSong:loudly: method to the outside world.
     That method is internal to our class. So why should we expose it? */
@end

 


鉴于此接口文件,程序员可以调用walkAtKilometersPerHour:从Person类外runAt10KilometersPerHour方法,而不是单调的:

 singSong:loudly:方法,因为它并没有在该文件中公开。因此,让我们继续前进,尝试调用所有这三种方法从我们的应用程序委托,看看会发生什么! 

- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
        Person *person = [[Person alloc] init];
        [person walkAtKilometersPerHour:3.0f];
        [person runAt10KilometersPerHour];
        /* If you uncomment this line of code, the compiler will give
         you an error telling you this method doesn‘t exist on the Person class */
 /*如果去掉这一行代码,编译器会给出 
         你的错误,告诉你这种方法不会对Person类存在*/
//[person singSong:nil loudly:YES]; self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; return YES; }

 

 


现在我们知道了如何定义和调用实例方法,但对于类的方法?让我们先了解一下什么类的方法,他们从实例方法的区别。
一个实例方法是,涉及到一个类的实例方法。例如,在我们的Person类,你可以实例化该类两次来创建您正在使用两个不同的人在一个假设的游戏,有那些人之一,走在一个时刻3公里的速度,而其他人走在2公里的时速。
即使你写的代码的步行实例方法只有一次,当在运行时会创建两个单独的Person类的实例时,调用实例方法将被路由到这个类的相应实例。
相反,类方法的类本身的工作。例如,在一个游戏,你有点亮你的游戏风景类名为光之情况下,你可能有一个dimAllLights类方法在这个类,程序员可以调用昏暗的灯光都在游戏中,无论他们在哪里放置。让我们一起来看看我们的Person类一个类的方法的例子:
   

#import "Person.h"
@implementation Person
+ (CGFloat) maximumHeightInCentimeters{ return 250.0f;
}
+ (CGFloat) minimumHeightInCentimeters{ return 40.0f;
}
@end

 


 该maximumHeightInCentimeters方法是一个类方法,它返回的任何人以厘米为单位的假想最大高度。该minimumHeightInCentim ETERS类方法返回的任何人的最低高度。下面是我们如何再暴露在我们的类的接口这些方法:

#import <Foundation/Foundation.h>
@interface Person : NSObject
@property (nonatomic, copy) NSString *firstName; @property (nonatomic, copy) NSString *lastName; @property (nonatomic, assign) CGFloat currentHeight;
+ (CGFloat) maximumHeightInCentimeters; + (CGFloat) minimumHeightInCentimeters;
@end

 

    
我们还为我们命名currentHeight Person类增加了新的浮点性能。这允许这个类的实例,以便能够存储其高度在存储器中用于以后参考,就像他们的第一或最后一个名称。
而在我们的应用程序的委托,我们将继续使用这些新的方法,如下所示:
??

- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
Person *steveJobs = [[Person alloc] init]; steveJobs.firstName = @"Steve";
steveJobs.lastName = @"Jobs"; steveJobs.currentHeight = 175.0f; /* Centimeters */
if (steveJobs.currentHeight >= [Person minimumHeightInCentimeters] && steveJobs.currentHeight <= [Person maximumHeightInCentimeters]){
/* The height of this particular person is in the acceptable range */
}else{
/* This person‘s height is not in the acceptable range */
}
        self.window = [[UIWindow alloc]
                       initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible];
return YES;
}

 

1.4为类添加功能 :类方法和实例方法

标签:style   blog   http   color   io   os   ar   使用   for   

原文地址:http://www.cnblogs.com/shuozi-love/p/4034559.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!