No one in the group will use the IPHONE version of the APP. After all, the IPHONE development is very difficult for students, mainly because of the hardware cost. a mac computer costs more than 10 thousand, children of ordinary people cannot afford it. Therefore, as long as you learn it with a strong scalp, you will also write your study notes for your reference.
First, let's talk about the definition of classes. In Object-C, an interface must be defined first. This interface is used to describe the composition of this class, including member variables, class variables, class methods, and member methods. The extension of the interface file is h, which is defined as the header file in C language.
The following is a class definition. The function of this class is to use two int-type numbers to form a score.
Fraction. h
# Import <Foundation/Foundation. h>
Static int t = 0
@ Interface Fraction: NSObject {
Int numerator; // molecule
@ Public int denominator; // denominator
}
-(Void) setNumerator: (int) numerator; // setter method of numerator
-(Void) setDenominator: (int) denominator; // The setter method of denominator
-(Void) setNumerator: (int) numerator andDenominator: (int) denominator;
// A shortcut for setting two member variables at the same time
-(Int) numerator; // The getter method of numerator
-(Int) denominator; // The getter method of denominator
-(Void) print;
+ (Void) t;
@ End
An interface in object c is a HEADER file in c. The structure of this file is as follows:
Let's take a line-by-line look at the above content:
(1) What is different from the C language here is that the import header file uses import instead of include. In addition, if you want to find the Header file from the current directory, you can find it in the system's Header file library, use # import "Header file". If you only want to find it from the system's Header file library, use # import <Header file>. Foundation/Foundation. h contains all header file definitions in Foundation Kit. The GNUStep Object-C header file is in the \ GNUStep installation directory \ GNUstep \ System \ Library \ Headers \ Foundation folder. The AppKit header file of the GNUStep Object-C is in
\ GNUStep installation directory \ GNUstep \ System \ Library \ Headers \ AppKit folder.
(2) The static class variables are defined outside the interface. class variables can only be accessed by this class unless class methods are provided to the outside.
Access this class variable.
(3 .) the @ + command in Object-C Represents the derivative Syntax of Object-C other than C. Therefore, @ interface defines an interface, and the interface name is followed by a colon, the colon is the name of the parent class, and the top-level parent class in Object-C is NSObject.
(4) The interface definition is followed by a pair of {}, which defines the member variables. The so-called member variables are equivalent
Instance variable, from the object of the class. The member variables in Object-C Use @ public, @ protected, and @ private as access modifiers. The default value is @ protected. Here, you need to know that only the member variables in Object-C have access modifiers. class variables, class methods, and member methods do not have access modifiers. All methods are public, all class variables are private.
(5) The method starting with-is a member method, the method starting with + is a class method, and the type description in the method (return value type,
Parameter type) must be enclosed. If the method has multiple parameters, each parameter has a tag name (Yes
Omitted, but it is not recommended to do so). Each label name is separated by a colon. If you have multiple parameters
Method, the actual method name is method name: Tag Name 1: Tag name 2 :... ..., The above square with two parameters
Method Name: setNumerator: andDenominator :. Unlike JAVA
Class methods can only be called by class. If you call an object, an error is reported, while JAVA only provides a warning during compilation.
(6) @ end indicates that the interface definition ends. This is because, unlike JAVA, JAVA's type definition is surrounded,
The {} in Object-C only contains member variables, so an end sign must be provided. Generally, JAVA programmers often forget
Write the end mark.
Here you need to know that the @ interface of Object-C is not the same as the interface of JAVA.
@ Protocol in Object-C is equivalent to interface in JAVA. Here, you only need to remember Object-C
@ Interface in is only a description of the class, because @ interface is usually in an independent h file, you can put it class
Similar to the function prototype in C, that is, the class prototype in Object-C. Through this prototype, compile
You can know the functions of the specific implementation class.
The method in the above interface is very simple, mainly the setter and getter methods of the member variables, which has nothing to do with JAVA
Different. However, you will find that the getter method does not use get as the method name prefix, because the method starting with get
It has a special meaning in Object-C, which will be seen later.
From TQUDING's column