Objective-C Class-objective-C methods with multiple parameters

Source: Internet
Author: User
Let's continue to talk about the fraction class in objective-C separation interface and implementation file. If there is a way to set numerator with only one message
At the same time, you can set denominator. How nice is that! No problem. By listing each continuous method and connecting it with a colon, you can define an accept Multiple ParametersOf Method. Parameters connected by colons are part of the method name. For example, the method name addentrywithname: andemail indicates the method for receiving two parameters, which may be the name and email address. Method
Addentrywithname: andemail: andphone: a method that receives the following three parameters: a name, an email address, and a phone number.
At the same time, the numerator and denominator methods can be set to setnumerator: anddenominator: You can take the following form:
[Myfraction setnumerator: 1 anddenominator: 3];
This method is not bad. In fact, this is the preferred method for naming. However, you must specify a name that is easier to read for the method. For example, setto: over is a more general practice, which is more readable than the previous one.
[Myfraction setto: 1 over: 3];
Next, let's apply this new method to fraction. h:
01 # Import
<Foundation/Foundation. h>
02
03 // Define the fraction class
04
05 @ Interface Fraction: Nsobject
06 {
07         Int Numerator;
08         Int Denominator;
09 }
10
11 @ Property Int Numerator, Denominator;
12
13 -(Void)               Print;
14 -(Void)               Setto: (INT) N Over: (INT) D;
15 -(Double)       Converttonum;
16
17 @ End
Then, add a new method definition in the implementation file:
01 # Import
"Fraction. H"
02
03 @ Implementation Fraction
04
05 @ Synthesize Numerator, Denominator;
06
07 -(Void) Print
08 {
09         Nslog (@ "% I/% I ", Numerator, Denominator );
10 }
11
12 -(Double) Converttonum
13 {
14         If (Denominator ! = 0)
15                 Return (Double) Numerator / Denominator;
16         Else
17                 Return 1.0;
18 }
19
20 -(Void) Setto: (INT) N Over: (INT) D
21 {
22         Numerator = N;
23         Denominator = D;
24 }
25
26 @ End
Finally, apply it in Main. M:
01 # Import
"Fraction. H"
02
03 Int Main (Int Argc, Const Char * Argv [])
04 {
05         NSAID utoreleasepool * Pool = [[NSAID utoreleasepool Alloc] Init];
06         Fraction * Afraction = [[Fraction Alloc] Init];
07        
08         [Afraction Setto: 100 Over: 200];
09         [Afraction Print];
10        
11         [Afraction Setto: 1 Over: 3];
12         [Afraction Print];
13         [Afraction Release];
14        
15         [Pool Drain];
16         Return 0;
17 }
Example 7-2 is completed. The final output result is as follows:

100/200

1/3


Apple developer Mike Source Code address uploaded on csdn.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.