How do you define a class in Objective-c? We can use the following format to represent:
[CPP]View Plaincopy
- @interface Class Name: Parent class Name {
- variable definition;
- }
- method definition;
- @end;
An example is given below:
[CPP]View Plaincopy
- @interface Person:nsobject {
- nsstring* name;
- int age;
- }
- -(nsstring*) name;
- -(int) age;
- -(void) SetName: (nsstring*) input;
- -(void) Setage: (int) input;
- @end;
The class definition starts with @interface, which represents the interface declaration of the class, specifies the parent class of the class after the colon, @end, and indicates the end of the class definition.
The variable definition of a class in a Objective-c class definition is separate from the method definition, the definition of the variable is written in curly brackets, and the method definition of the class is written outside the curly braces. The method definition is unique and has the following format:
-(void) SetName: (nsstring) input;
Method type return type method name method accept parameter parameter type parameter name
The general format is as follows:
+/-(return type) name Sub 1: (Type 1) parameter 1 Name 2: (type 2) Parameter 2 ...
Corresponding with the + number, which represents the static method of the class, does not need to be instantiated to invoke.
A method definition for a parameter:
-(void) SetName: (nsstring*) input;
Method definitions for two parameters:
-(void) SetName: (nsstring*) input andsecondname: (nsstring*) input1;
Objective-c class Interface (@interface) (class definition)