Learning Object-C from scratch --- the fourth day (3), object-c --- the fourth day
Today, we will first summarize the previous data types:
Note: The id data type is a special data type of OC. It can be used to store any data type and is the basis of polymorphism and dynamic binding.
Start a new explanation-arithmetic expressions
A piece of code begins today's learning:
1 // 2 // main. m 3 // Demo5 4 // 5 // Created by lee on 14/11/6. 6 // Copyright (c) 2014 lee. all rights reserved. 7 // 8 9 # import <Foundation/Foundation. h> 10 11 int main (int argc, const char * argv []) {12 @ autoreleasepool {13 int a = 100; 14 int B = 2; 15 int c = 25; 16 int d = 4; 17 int result; 18 result = a-B; 19 NSLog (@ "a-B = % I", result ); 20 21 result = B * c; 22 NSLog (@ "B * c = % I", result); 23 24 result = a/c; 25 NSLog (@ "a/c = % I", result); 26 27 result = a + B * c; 28 NSLog (@ "a + B * c = % I", result); 29 30 result = a * B + c * d; 31 NSLog (@ "a * B + c * d = % I", result); 32} 33 return 0; 34} 35 output result: 36 01:16:40. 458 Demo5 [651: 34029] a-B = 9837 01:16:40. 458 Demo5 [651: 34029] B * c = 5038 01:16:40. 458 Demo5 [651: 34029] a/c = 439 01:16:40. 459 Demo5 [651: 34029] a + B * c = 15040 01:16:40. 459 Demo5 [651: 34029] a * B + c * d = 30041 Program ended with exit code: 0View Code
Note: In OC, the algorithm priority is the same as that in other languages. The algorithm priority is first multiplied by division, followed by addition or subtraction. The brackets can change the computing order.
Use the arithmetic expression rules to write the calculator class to implement basic arithmetic:
First, define the interface: including the initialization (clear), get set method, add (add) subtraction (subtract) multiplication (mutiply) Division (devide) method definition
1 // 2 // NSObject + Calculator. h 3 // Demo5 4 // 5 // Created by lee on 14/11/6. 6 // Copyright (c) 2014 lee. all rights reserved. 7 // 8 9 # import <Foundation/Foundation. h> 10 11 @ interface Calculator: NSObject12 {13 double accumulator; 14} 15 // accmulator16-(void) setAccumulator: (double) value; 17-(void) clear; 18-(double) getAccumulator; 19 20 // arithetic methods21-(void) add: (double) value; 22-(void) subtract: (double) value; 23-(void) multiply: (double) value; 24-(void) devide: (double) value; 25 @ endView Code
Then define the implementation class to implement the methods defined in the interface
1 // 2 // NSObject + Calculator. m 3 // Demo5 4 // 5 // Created by lee on 14/11/6. 6 // Copyright (c) 2014 lee. all rights reserved. 7 // 8 9 # import "NSObject + Calculator. h "10 11 @ implementation Calculator12-(void) setAccumulator :( double) value13 {14 accumulator = value; 15} 16 17-(void) clear18 {19 accumulator = 0; 20} 21 22-(double) getAccumulator23 {24 return accumulator; 25} 26 27-(void) add :( double) value28 {29 accumulator + = value; 30} 31 32-(void) subtract :( double) value33 {34 accumulator-= value; 35} 36 37-(void) multiply :( double) value38 {39 accumulator * = value; 40} 41 42-(void) devide :( double) value43 {44 accumulator/= value; 45} 46 47 @ endView Code
Final method call and output result
1 // 2 // main. m 3 // Demo5 4 // 5 // Created by lee on 14/11/6. 6 // Copyright (c) 2014 lee. all rights reserved. 7 // 8 9 # import <Foundation/Foundation. h> 10 # import "NSObject + Calculator. h "11 12 int main (int argc, const char * argv []) {13 @ autoreleasepool {14 Calculator * deskCalc; 15 deskCalc = [[Calculator alloc] init]; 16 17 [Export calc clear]; 18 [Export calc setAccumulator: 10]; 19 NSLog (@ "The result id % g", [Export calc getAccumulator]); 20 [Export calc add: 200]; 21 NSLog (@ "The result id % g", [using calc getAccumulator]); 22 [using calc devide: 15.0]; 23 NSLog (@ "The result id % g", [using calc getAccumulator]); 24 [using calc subtract: 10.0]; 25 NSLog (@ "The result id % g ", [using calc getAccumulator]); 26 [using calc multiply: 5]; 27 NSLog (@ "The result is % g", [using calc getAccumulator]); 28} 29 return 0; 30} 31 output result: 32 00:50:19. 874 Demo5 [601: 25952] The result id 1033 00:50:19. 875 Demo5 [601: 25952] The result id 21034 00:50:19. 875 Demo5 [601: 25952] The result id 1435 00:50:19. 875 Demo5 [601: 25952] The result id 436 00:50:19. 875 Demo5 [601: 25952] The result is 2037 Program ended with exit code: 0View Code
Category and category legal and use reference: http://www.cnblogs.com/likun-java/p/4058448.html
How to Learn object-c
Buy this document. It would be easier if you have learned java. Fuel
What level can I reach when I start learning Junior High School Mathematics for four and a half hours a day in three months?
~~ If you are a boy, your understanding skills are usually better than that of girls.
~~ Don't worry if you are a girl ~~
~~ You learned from scratch ~~ However, before junior high school, 'If you have been careful, you can finish learning in three days ~~ # The first day and the second day are at most one and a half minutes ~~
~~ I thought it was just a breeze, so the concept must be: carefully: look at it.
~~ Never careless ~~ This is too important for future studies.
~~ As long as you find the learning focus, you can learn faster.
~~ It takes about one and a half months for you to finish all the junior high school courses.
~~ It is not difficult because the high school curriculum is just basic knowledge.
~~ You can watch it in three weeks ~~ At this moment, you are looking at junior high school students ~~
It will take about a week to review it ~~
You are learning ~~ High school courses ~~ It takes a month to learn well.
~~~ It's three months since I finished my sophomore year.
~~~~~ Because your foundation is not solid ~~ After you finish learning the course, check whether you want to learn it ~~~