1. Trang chủ >
  2. Công Nghệ Thông Tin >
  3. Kỹ thuật lập trình >

13  Defining Two or More Methods with the Same Name

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (24.65 MB, 900 trang )


This example shows a typical pattern in overloading. Each rectangle can be drawed

either filled (solid color) or empty (showing just its boundaries). The first procedure is

a "convenience procedure" that allows the caller to avoid specifying how to fill the

rectangle. In our implementation of the first procedure, we merely call the second procedure, making the choice for the caller (andFilled:YES) The second procedure gives

the caller control over filling.



Discussion

Method overloading is a programming language feature supported by Objective-C,

C++, Java, and a few other languages. Using this feature, programmers can create

different methods with the same name, in the same object. However, method overloading in Objective-C differs from that which can be used in C++. For instance, in

C++, to overload a method, the programmer needs to assign a different number of

parameters to the same method and/or change a parameter’s data type.

In Objective-C, however, you simply change the name of at least one parameter.

Changing the type of parameters will not work:

- (void) method1:(NSInteger)param1{

/* We have one parameter only */

}

- (void) method1:(NSString *)param1{

/* This will not compile as we already have a

method called [method1] with one parameter */

}



Changing the return value of these methods will not work either:

- (int) method1:(NSInteger)param1{

/* We have one parameter only */

return param1;

}

- (NSString *) method1:(NSString *)param1{

/* This will not compile as we already have a

method called [method1] with one parameter */

return param1;

}



As a result, you need to change the number of parameters or the name of (at least) one

parameter that each method accepts. Here is an example where we have changed the

number of parameters:



1.13 Defining Two or More Methods with the Same Name | 47



www.it-ebooks.info



- (NSInteger) method1:(NSInteger)param1{

return param1;

}

- (NSString*) method1:(NSString *)param1

andParam2:(NSString *)param2{

NSString *result = param1;

if ([param1 length] > 0 &&

[param2 length] > 0){

result = [result stringByAppendingString:param2];

}

return result;

}



Here is an example of changing the name of a parameter:

- (void) drawCircleWithCenter:(CGPoint)paramCenter

radius:(CGFloat)paramRadius{

/* Draw the circle here */

}

- (void) drawCircleWithCenter:(CGPoint)paramCenter

Radius:(CGFloat)paramRadius{

/* Draw the circle here */

}



Can you spot the difference between the declarations of these two methods? The

first method’s second parameter is called radius (with a lowercase r) whereas the second

method’s second parameter is called Radius (with an uppercase R). This will set these

two methods apart and allows your program to get compiled. However, Apple has

guidelines for choosing method names as well as what to do and what not to do when

constructing methods. For more information, please refer to the “Coding Guidelines

for Cocoa” Apple documentation available at this URL:

http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/Co

dingGuidelines/CodingGuidelines.html

Here is a concise extract of the things to look out for when constructing and working

with methods:

• Have your method names describe what the method does clearly, without using

too much jargon and abbreviations. A list of acceptable abbreviations is in the

Coding Guidelines.



48 | Chapter 1: The Basics



www.it-ebooks.info



• Have each parameter name describe the parameter and its purpose. On a method

with exactly three parameters, you can use the word and to start the name of the

last parameter if the method is programmed to perform two separate actions. In

any other case, refrain from using and to start a parameter name.

• Start method names with a lowercase letter.

• For delegate methods, start the method name with the name of the class that invokes that delegate method.



See Also

Recipe 1.12



1.14 Allocating and Initializing Objects

Problem

You want to create an instance of a new object, but you don’t understand the difference

between allocation and initialization and why you should have to both allocate and

initialize an object before you can use it.



Solution

You must both allocate and initialize an object before using it. An object can be allocated using the alloc instance method. This class method will allocate memory to hold

the object and its instance variables and methods. However, allocation leaves memory

undefined. So in addition, each object must be initialized, which sets the values of its

data. One initialization method must be the designated initializer, which is normally

the initialization method with the most parameters. For instance, the initWithFrame:

method is the designated initializer of objects of type UIView. Always allocate and initialize your objects, in that order, before using them.

When implementing a new object, do not override the alloc method. This method is

declared in NSObject. Instead, override the init method and create custom initialization

methods that handle required parameters for the specific object you are working on.



Discussion

An object that inherits from NSObject must be prepared for use in two steps:

1. Allocation

2. Initialization

The allocation is done by invoking the alloc method, which is implemented in the

NSObject class. This method creates the internal structure of a new object and sets all

instance variables’ values to zero. After this step is done, the init method takes care of



1.14 Allocating and Initializing Objects | 49



www.it-ebooks.info



setting up the default values of variables and performing other tasks, such as instantiating other internal objects.

Let’s look at an example. We are creating a class named MyObject. Here is the .h file:

#import

@interface MyObject : NSObject

- (void) doSomething;

@end



The implementation of this class is as follows (the .m file):

#import "MyObject.h"

@implementation MyObject

- (void) doSomething{

/* Perform a task here */

NSLog(@"%s", __FUNCTION__);

}

@end



The doSomething instance method of the MyObject object will attempt to print the name

of the current function to the console window. Now let’s go ahead and invoke this

method by instantiating an object of type MyObject:

MyObject *someObject = [[MyObject alloc] init];

/* Do something with the object, call some methods, etc. */

[someObject doSomething];



This code will work absolutely fine. Now try to skip initializing your object:

MyObject *someObject = [MyObject alloc];

/* Do something with the object, call some methods, etc. */

[someObject doSomething];



If you run this code now, you will realize that it works absolutely fine, too. So, what

has happened here? We thought we had to initialize the object before we could use it.

Perhaps Apple can explain this behavior better:

An object isn’t ready to be used until it has been initialized. The init method defined in

the NSObject class does no initialization; it simply returns self.



Simply put, this means the init method is a placeholder for tasks that some classes

need to perform before they are used, such as setting up extra data structures or opening

files. NSObject itself—along with many of the classes you will use—does not have to

initialize anything in particular. However, it is a good programming practice to always

run the init method of an object after allocating it in case the parent of your class has

overridden this method to provide a custom initialization. Please bear in mind that the



50 | Chapter 1: The Basics



www.it-ebooks.info



Xem Thêm
Tải bản đầy đủ (.pdf) (900 trang)

×