Now we'll use objective-c to define an actual class and learn how to use an instance of the class.
Again, we will learn the process first. Therefore, the actual procedural paradigm may not be particularly practical, and those more practical content will be discussed later.
Suppose you want to write a program for processing fractions, you might need to handle addition, subtraction, multiplication, and divide operations. If you don't yet know what a class is, you can start with a simple program with the following code:
Code Listing 3-1
A simple program that uses fractions
#import <Foundation/Foundation.h>
int main (int argc, char * argv[])
{
@autoreleasepool {
int numerator = 1;
int denominator = 3;
NSLog (@ "The fraction is%i/%i", numerator, denominator);
}
return 0;
}
Code Listing 3-1 Output
The fraction is 1/3
In code listing 3-1, fractions are expressed in the form of numerator and denominator. After the @autoreleasepool instruction, the first two lines in main are declared as integral types, and the variables numerator and denominator are given an initial value of 1 and 3, respectively. These two programs are equivalent to the following program line:
int numerator, denominator;
numerator = 1;
denominator = 3;
Store 1 in the variable numerator and store 3 in the variable denominator, which represents the score 1/3. This approach can be cumbersome if you need to store multiple scores in your program. Each time you want to refer to a score, you must refer to the numerator and denominator, and it is very difficult to manipulate the scores.
If you can define a fraction as a single entity. It is better to use a single name (for example, myfraction) to reference its numerator and denominator together. This approach can be implemented using OBJECTIVE-C, starting with defining a new class.
Code Listing 3-2 overrides the function in code listing 3-1 through a new class called Fraction. The program is given below, which will then detail how it works.
Code Listing 3-2
Programs that use fractions--class versions
#import <Foundation/Foundation.h>
----@interface Part----
@interface Fraction:nsobject
-(void) print;
-(void) Setnumerator: (int) n;
-(void) Setdenominator: (int) D;
@end
----@implementation Part----
@implementation Fraction
{
int numerator;
int denominator;
}
-(void) print
{
NSLog (@ "%i/%i", numerator, denominator);
}
-(void) Setnumerator: (int) n
{
Numerator = n;
}
-(void) Setdenominator: (int) d
{
denominator = D;
}
@end
----Program section----
int main (int argc, char * argv[])
{
@autoreleasepool {
Fraction *myfraction;
Create a fraction instance
myfraction = [Fraction alloc];
Myfraction = [myfraction init];
Set score to 1/3
[Myfraction setnumerator:1];
[Myfraction Setdenominator:3];
displaying fractions using the Print method
NSLog (@ "The value of Myfraction is:");
[Myfraction print];
}
return 0;
}
Code Listing 3-2 Output
The value of Myfraction is:
1/3
As you can see from the comments in code listing 3-2, the program is logically divided into the following 3 sections:
@interface section
@implementation section
Program section
Where the @interface section describes the methods of classes and classes, the @implementation section is used to describe the data (data stored by instance variables of the class object) and to implement the actual code that declares the method in the interface, and program code for the program section implements the intended purpose of the procedure
Attention
You can also declare instance variables for a class in the interface (interface) section. Starting with Xcode 4.2, you can already add instance variables to the implementation (Implementation) section in order to be able to define classes in a better way. The reasons are explained in the later chapters.
The above 3 sections exist in each OBJECTIVE-C program, even though you may not need to write each part yourself. You will see that each part is usually placed in its own file. For now, however, we put them in a separate file.
This article is excerpted from the OBJECTIVE-C Program Design (4th edition)
Publishing Industry Publishing House
Beauty Stephen G. Kochan (Stephen F. G. Kochan)
Lin Yu noritoshi Zhu Yixin Translation