Object-Oriented Programming
Basic:
- Concept: Object class Package Polymorphism
- The main languages are C++,JAVA,OC
Process-oriented and object-oriented differences
|
Process oriented |
Object Oriented |
| Characteristics |
Analysis steps, Implementing functions |
Analyze problems to identify participation and draw objects and their role |
| Focuses on |
Implementation features |
Object design (refers to function) |
| Example |
C language |
Oc,c++,java |
Basic code:
//OC Print NSLog(@"Lanou");//Nsinteger integer, cgfloat float, nsstring string Nsintegeri = -;NSLog(@"i =%ld", i);CGFloatf =3.14;NSLog(@"F =%g", f);//OC string can be put in Chinese NSString*str= @"Yo Yo";NSLog(@"%@", str);array of//OC //As for loop traversal, traverse directly with NSLog Nsarray*arr = @[@"1",@"2",@"3"];NSLog(@"%@", arr); for(Nsintegeri =0; I <3; i++) {NSLog(@"%@", Arr[i]); }
Classes and Object classes
- Classes and objects are the core of object-oriented programming
- A class is an abstraction that has the same characteristics and behavioral things.
Object
- Object is an instance of a class
- Class is the type of object
Definition of class in OC
- Define the class first, then create the object, and then use the object
- Definition has two parts, interface and implementation (note separate write)
- File:. h for the interface file,. m for the implementation file
Interface
@interface Student : NSObject@end
Realize
// 对应的实现文件@implementation Student
Creating objects
- Any object takes up space
- Create objects in two steps
- ① Allocating space
- ② initialization
- Initialization
// 1.分配空间Student *stu = [Student alloc];// 2.初始化stu = [stu init];// 可以把上两个步骤合成一个语句Student *stu = [[Student alloc] init];
The self that is returned in initialization refers to the return of the completed
- (id)init{ _stuName = @"俊宝宝"; _stuSex = @"女"; _stuHobby = @"男"; 20; 100; returnself;}
Working with objects
Student *stu = [[Student alloc] init]; [stu sayHi];
Instance variable operation
- Instance variables are initialized with only a small amount of setup, and later perfect
- The visibility of instance variables is divided into three types
- @public Public
- @private Private
- @protected protected
@interface Student : NSObject{ // 成员变量的可见度 @public // 成员变量,或实例变量 NSString// 名字 NSInteger _stuAge; // 年龄 NSString *_stuSex; // 性别 CGFloat _stuScore; // 分数 NSString *_stuHobby;// 爱好}@end
Action Members
// 操作成员变量 // 对象通过->来访问自己的成员变量 NSLog(@"%ld"-> _stuAge); // 修改年龄 -> =100; NSLog(@"%ld"-> _stuAge); // 修改姓名,直接修改字符串直接赋值 -> = @"切克闹"; NSLog(@"%@"-> _stuName);
Note: the instance variable of the public adornment can be accessed directly using the
Part of the Code
Main.m
int main(int argc, const char * argv[]) { // 通过手机的类,创建对象,并且对对象的成员变量进行修改 MobilePhone *mPhone = [[MobilePhone alloc] init]; mPhone -> _phoneBrand = @"samsung"; mPhone -> _phoneColor = @"白色"; mPhone -> _phoneModel = @"S100"; 4800; 1080; NSLog(@"品牌为%@,颜色:%@,型号:%@,价格:%ld,屏幕:%ld",mPhone->_phoneBrand,mPhone->_phoneColor,mPhone->_phoneModel,mPhone->_phonePrice,mPhone->_phoneSize); 0;}
MobilePhone.h
@interface mobilephone : nsobject { @public nsstring *_phonebrand; nsstring *_phonemodel; nsinteger _phonesize; nsstring *_phonecolor; nsinteger _phoneprice;} -(void ) buyphone;-(void ) call;-(void ) sendmessage;-(void ) surfinternet;-(void ) Watchvideo; //write a function to print all the information -(void ) Sayhi; @end
Mobilephone.m
@implementation mobilephone - (void) buyphone{NSLog(@"bought a cell phone.");} - (void) call{NSLog(@"Make a phone call.");} - (void) sendmessage{NSLog(@"sent a message.");} - (void) surfinternet{NSLog(@"on a net.");} - (void) watchvideo{NSLog(@"I saw a video.");}//Rewrite the phone's initialization method- (ID) init{_phonebrand = @"Apple"; _phonecolor = @"Red"; _phonemodel = @"S5"; _phoneprice =4000; _phonesize = the;return Self;} - (void) sayhi{NSLog(@"%@,%@,%@,%ld,%ld,", _phonebrand,_phonecolor,_phonemodel,_phoneprice,_phonesize);}
This article summarizes the basic contents of today's study
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
OC Learning 01-Classes and objects