Objective-c notes two classes, objects, and methods

Source: Internet
Author: User



An object is an item. Object-oriented programming can be seen as an artifact and something you want to do with it. This differs from the C language in that C is often referred to as procedural language. In C, it is usually the first thing to think about what to do before you focus on the object, which is almost always the opposite of the thought process of the face object.



In object-oriented terms, (here we use cars as objects) your car is an example of a car. Car is the name of the class, and this instance is created from that class. Therefore, every new car created creates a new instance of the car class, and each instance of the car is called an object.


Object Actions performed using an object
Your car. Driving
Come on
Car Wash
Maintenance





Each instance or object contains not only information about the original attributes obtained from the manufacturer, but also its current attributes, which can be changed dynamically. When you drive a car, the oil in the mailbox runs out, the car gets dirtier and the tires wear out.



Object usage methods can affect the state of an object. If the method is "refueling the car", then after the implementation of this method, the car's mailbox will be filled. This method affects the status of the car mailbox.



The key concept here is that the object is a unique representation of the class, and each object contains some information that is usually private to the object. Method provides the means to access and change this data.






[Classorinstance Method];



After the left parenthesis in this statement is followed by the name of the class or the instance name of the class, it can be followed by one or more spaces, followed by a space that will be executed. Finally, use the closing brackets and semicolons to terminate. When a class or instance is requested to perform an operation, it sends a message to it, and the recipient of the message is called the receiver. Therefore, there is another way to represent the general format described earlier:



[Receiver message];



Review the previous list and use this new syntax to write all the methods for it. Before that, you need to get a new car and go to the factory to buy one:



Yourcar = [Car new]; Get a new car//This is written in real detail, not a new object! Is Yourcar is now your car, you can use it to cite examples of cars.



With a new car there is a way to have a new car, so this new method can be called a factory method, or a class method:



[Yourcar Prep]; Get it ready for first-time use



[Yourcar Drive]; Drive your car



[Yourcar Wash]; Wash your car



[Yourcar Getgas]; Put gas in your car if you need it



[Yourcar service]; Service Your car






[Yourcar Topdown]; If it ' s a convertible



[Yourcar TopUp];



Currentmileage = [Yourcar odometer];



The last example shows an instance method that returns information, that is, the current mileage, which is seen through the odometer (odometer). We store this information inside the Currentmileage variable in the program.



Here is an example of a specific value as a method parameter, which differs from a method's direct invocation:



[Yourcar setspeed:55]; Set the speed-to-mph



Your sister Sue can use the same method for her own car instance;



[Suescar Drive];



[Suescar Wash];



[Suescar Getgas];



Applying the same method to different objects is one of the main concepts behind object-oriented programming.






Objective-c class for processing fractions



Suppose you want to write a program that processes fractions, you might need to handle addition, subtraction, multiplication, and divide operations. A fraction class was created:


1 //
 2 // Fraction.h
 3 //
 4 // Created by on 15/11/13.
 5 // Copyright © 2015. All rights reserved.
 6 // interface section
 7
 8 #import <Foundation / Foundation.h>
 9 
10 @interface Fraction: NSObject
11
12-(void) print;
13-(void) setNumerator: (int) n;
14-(void) setDenominator: (int) d;
15-(int) numberator;
16-(int) denominator;
17
18 @end
 1 //
 2 // Fraction.m
 3 // HelloWorld
 4 //
 5 // Created by on 15/11/13.
 6 // Copyright © 2015. All rights reserved.
 7 //
 8 
 9 #import "Fraction.h"
10
11 @implementation Fraction
12 {
13 int numberator;
14 int denominator;
15}
16-(void) print {
17 NSLog (@ "% i /% i", numberator, denominator);
18}
19-(void) setNumerator: (int) n {
20 numberator = n;
twenty one }
22-(void) setDenominator: (int) d {
23 denominator = d;
twenty four }
25-(int) numberator
26 {
27 return numberator;
28}
29-(int) denominator
30 {
31 return denominator;
32}
33 @end
34 // ---- program section ----
35 #import <Foundation / Foundation.h>
36 int main (int argc, const char * argv [])
37 {
38 @autoreleasepool {
39 Fraction * myFraction;
40
41 // Create a score instance alloc allocate space init initialization
42 myFraction = [[Fraction alloc] init];
43 // myFraction = [myFraction init];
44
45
46 // Fraction * fact = [[Fraction alloc] init];
47
48 // Set the score to 1/3
49 [myFraction setNumerator: 1];
50 [myFraction setDenominator: 3];
51
52 // [fact setDenominator: 3];
53 // [fact setNumerator: 7];
54
55 // Use the print method to display the score
56 NSLog (@ "The value of myFraction is:% i /% i", [myFraction numberator], [myFraction denominator]);
57 // [myFraction print];
58 //
59 // NSLog (@ "Second fraction is:");
60 // [fact print];
61}
62 return 0;
63} 


The @interface section describes the methods used to describe classes and classes, and the @implementation section is used to describe data (data stored by the power variables of the class object) and to implement instance code that declares the method in the interface;






@interface section



The general format resembles the following:



@interface Newclassname:parentclassname



Propertyandmethoddeclarations;



@end



By convention, class names begin with uppercase letters.


    • Name rules for variable names: Names must begin with a letter or underscore (_), followed by any combination of numbers (uppercase or lowercase) letters, underscores, or 0~9.
    • Some names cannot be used for variable names, such as Int. Because its purpose has a special meaning for the objective-c compiler, this usage is called a reserved name or reserved word. (is the system keyword)
    • The uppercase and lowercase letters in the objective-c are distinguished, and the variable names sum, sum, and sum all represent different variables.


Class methods and Instance methods



-(void) print;



The beginning of the minus sign (-) notifies the Objective-c compiler that the method is an instance method. In addition, there is a choice that is just (+), which represents the class method. A class method is a method that performs some action on the class itself, such as creating a new instance of a class.



1. Return value



-(int) currentage;



When declaring a new method, you must tell the OC compiler whether the method has a return value. In parentheses is the method's return value type! The method specifies that an instance method named Currentage will return an integer value.



Example: Double doubles the value of void, which represents no return type.



2, the parameters of the method



-(void) Setnumberator: (int) n;



This is an instance method with no return value type, and int is used to indicate the parameter type. As far as Setnumberator is concerned, its parameter name is N. This name can be arbitrary.






@implementation section



The @implementation section contains the instance code of the method that is declared in the @interface section, and you need to specify the data type that is stored in the class object. (That is, the interface class, and the implementation class)



The general format of the @implementation section is as follows:



@implementation Newclassname



{



Memberdeclarations;



}



Methoddefinitions;



@end



The memberdeclarations section specifies what type of data will be stored in the fraction and the names of those data types.






Program section



The program section contains code that solves a specific problem, and it can span multiple files if necessary. Where there must be a function called main, usually this is where the program begins to execute.






Fraction *myfraction;



This line indicates that Myfraction is an object of type fraction.



Myfraction = [[Fraction alloc] init];



Alloc is the abbreviation for allocate because the new score is allocated memory storage space.



The Init method is used to initialize an instance variable of the class. Note that you are sending the INIT message to myfraction. In other words, to initialize a special fraction object here, it is not sent to the class, but to an instance of the class.



The Init method can also return a value, the object being initialized. Stores the return value in the variable myfraction of fraction.



Let's go back to fraction *myfraction;



  An asterisk (*) before myfraction indicates that Myfraction is a reference (or pointer) to the fraction object. The variable myfraction does not actually store the fraction data, but instead stores a reference (actually a memory address) that indicates where the object data is in memory.









All right! The other is the Set value method (setter) and the value method (getter), it is not explained in detail, it should be well understood.



  



Objective-c notes two classes, objects, and methods


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.