Now we will use objective-C to define an actual class and learn how to use the class instance.
Similarly, we will first learn the process. Therefore, the actual program examples may not be particularly practical, and more practical content will be discussed later.
If you want to write a program for processing scores, you may need to process addition, subtraction, multiplication, division, and other operations. If you do not know what a class is, you can start with a simple program. The Code is as follows:
Code List 3-1
// Simple program with scores
# 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 List 3-1 Output
The fraction is 1/3
In code listing 3-1, the score is represented in the form of numerator and denominator. After the @ autoreleasepool command, the first two rows of variables numerator and denominator in main are declared as integer, and they are assigned the initial values 1 and 3 respectively. These two programs are equivalent to the following program lines:
Int numerator, denominator;
Numerator = 1;
Denominator = 3;
Store 1 to the variable numerator and 3 to the variable denominator, which indicates the score of 1/3. If you need to store multiple scores in a program, this method may be cumbersome. Each time you want to reference a score, you must reference the corresponding numerator and denominator, and these scores are also quite difficult.
If you can define a score as a single entity. It would be better to use a single name (such as myfraction) to reference its numerator and denominator together. This method can be implemented using objective-C, starting from defining a new class.
Code listing 3-2 uses a new class named fraction to override the function in code listing 3-1. The following describes how the program works.
Code List 3-2
// Use the program with scores-class version
# 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 part ----
Int main (INT argc, char * argv [])
{
@ Autoreleasepool {
Fraction * myfraction;
// Create a score instance
Myfraction = [fraction alloc];
Myfraction = [myfraction init];
// Set the score to 1/3
[Myfraction setnumerator: 1];
[Myfraction setdenominator: 3];
// Display the score using the print method
Nslog (@ "the value of myfraction is :");
[Myfraction print];
}
Return 0;
}
Code List 3-2 output
The value of myfraction is:
1/3
From the comments of code listing 3-2, we can see that the program is logically divided into the following three parts:
@ Interface
@ Implementation
Program
Here, @ interface is used to describe classes and classes; @ implementation is used to describe data (data stored by instance variables of class objects ), and realize the actual code to declare the method in the interface; the program code in the program part achieves the intended purpose of the program.
Note:
You can also declare instance variables for the class in the interface (Interface) section. From xcode 4.2, you can add instance variables in implementation (Implementation) to define classes in a better way. The reason is described in later sections.
The preceding three parts exist in each objective-C program, even if you do not need to write each part yourself. You will see that each part is usually placed in its own file. However, for the moment, we put them in a separate file.
This article is excerpted from objective-C
Programming (version 4th)
Published by Electronic Industry Publishing House
[Us] Stephen G. kochan (Stephen G. cochang)
Lin Ji
Translated by fan Jun Zhu Yixin