Interpreting object-oriented ideas with 1 + 2 = 3
- Write a calculation method to calculate 1 + 2.
- Object-oriented, interpreting the object-oriented thinking of everything.
Ideas:
YGInteger. h
#import <Foundation/Foundation.h>@interface YGInteger : NSObject@property(nonatomic,assign)NSInteger integer;@end
Write a description method in YGInteger. m, and convert the number into a string in expansion.
#import "YGInteger.h"#import "YGInteger_YGChangeValue.h"@implementation YGInteger- (NSString *)description{ return [NSString stringWithFormat:@"%ld",self.integer];}- (NSString *)changeValue{ return [NSString stringWithFormat:@"%@",self];}@end
YGInteger + YGInit. h This is the Init class of Integer
#import "YGInteger.h"#import "YGInteger.h"@interface YGInteger (YGInit)- (instancetype)initWithInteger:(NSInteger)integer;+ (instancetype)integerWithInteger:(NSInteger)integer;@end
YGInteger + YGInit. m to implement the standard method
#import "YGInteger+YGInit.h"@implementation YGInteger (YGInit)- (instancetype)initWithInteger:(NSInteger)integer{ if (self = [super init]) { self.integer = integer; } return self;}+ (instancetype)integerWithInteger:(NSInteger)integer{ __autoreleasing YGInteger *i = [[YGInteger alloc]initWithInteger:integer]; return i;}@end
Calculation classification of YGInteger + YGMath. h Integer, which includes four methods of addition, subtraction, multiplication, and division.
#import "YGInteger.h"@interface YGInteger (YGMath)- (YGInteger *)add:(YGInteger *)i;- (YGInteger *)minus:(YGInteger *)i;- (YGInteger *)multiply:(YGInteger *)i;- (YGInteger *)divide:(YGInteger *)i;@end
YGInteger + YGMath. m use object calls to fully reflect object-oriented programming.
#import "YGInteger+YGMath.h"@implementation YGInteger (YGMath)- (YGInteger *)add:(YGInteger *)i{ YGInteger *result = [[YGInteger alloc]init]; result.integer = self.integer + i.integer; return result;}- (YGInteger *)minus:(YGInteger *)i{ YGInteger *result = [[YGInteger alloc]init]; result.integer = self.integer - i.integer; return result;}- (YGInteger *)multiply:(YGInteger *)i{ YGInteger *result = [[YGInteger alloc]init]; result.integer = self.integer * i.integer; return result;}- (YGInteger *)divide:(YGInteger *)i{ YGInteger *result =[[YGInteger alloc]init]; result.integer = self.integer / i.integer; return result;}@end
YGInteger_YGChangeValue.h. Finally, declare the conversion method in the extension method.
#import "YGInteger.h"@interface YGInteger ()- (NSString *)changeValue;@end
Drag a Label on the storyBoard to display the result and write a show method to display the data to the simulator.
#import "ViewController.h"#import "YGInteger+YGInit.h"#import "YGInteger+YGMath.h"@interface ViewController ()@property (weak, nonatomic) IBOutlet UILabel *outputLabel;- (void)show:(YGInteger *)i;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; YGInteger *int1 = [[YGInteger alloc]initWithInteger:1]; YGInteger *int2 = [[YGInteger alloc]initWithInteger:2]; YGInteger *int3 = [int1 add:int2]; self.outputLabel.text = [NSString stringWithFormat:@"%@",int3];// [self show:[int1 add:int2]];}- (void)show:(YGInteger *)i{ self.outputLabel.text = [NSString stringWithFormat:@"%@",i];}@end
- Use an object to add objects. The embodiment of the image comes forward to object programming. This is an embodiment of thought. The idea is inherent. It is difficult to add two numbers. Why don't we directly input two parameters? We need to use an object to call attributes, so we can just put off our pants and put it apart.
- Because, in the future, the demand is not just a single task. If you use an object to call, you will have many choices and will not be limited to one kind of thinking. If you really choose to think about the problem with a single mindset, you can only stay at 1 + 2 = 3 in the future.
Only.