简单的说就是将程序和数据封装在一起,从而提高软件的重用性,灵活性,和扩展性等!
三大特征:
1.封装 (Encapsulation)
2.继承 (lnheritance)
3.多态 (polymorphism)
封装:
//
// ViewController.m
// OOP
//
// Created by apple on 15/9/9.
// Copyright (c) 2015年 hell xin. All rights reserved.
//
#import "ViewController.h"
@interface Student : NSObject
{
// @private
// int _age;
}
- (void)startingUp;
- (void)startXcode;
- (void)Writethecode;
- (void)CompileTheProject;
//数据封装(Data Encapsulation)
//- (void)setAge:(int)age;
//- (int)age;
@property int age;//直接用属性代替!
@end
@implementation Student
//开机
- (void)startingUp
{
NSLog(@"1.开机");
}
//启动xcode
- (void)startXcode
{
NSLog(@"2.启动xcode");
}
//编写代码
- (void)Writethecode
{
NSLog(@"3.编写代码");
}
//编译项目
- (void)CompileTheProject
{
NSLog(@"编译项目");
}
//- (void)setAge:(int)age
//{
// if (age<0||age>200) {
// NSLog(@"年龄输入有错误");
// return;
// }
// _age=age;
//}
//
//-(int)age
//{
// return _age;
//}
@end
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
Student *student = [Student new];
[student startingUp];
[student startXcode];
[student Writethecode];
[student CompileTheProject];
//objective-c提供简便方法代理手动书写的setter和getter。
//并且可以通过点号运算符(.)调用setter和getter方法!
[student setAge:20];
NSLog(@"1.age:%d",[student age]);
student.age = 21;
NSLog(@"2.age:%d",student.age);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
//编译结果
2015-09-09 19:39:14.991 OOP[3212:247750] 1.开机
2015-09-09 19:39:14.992 OOP[3212:247750] 2.启动xcode
2015-09-09 19:39:14.992 OOP[3212:247750] 3.编写代码
2015-09-09 19:39:14.992 OOP[3212:247750] 编译项目
2015-09-09 19:39:14.992 OOP[3212:247750] 1.age:20
2015-09-09 19:39:14.994 OOP[3212:247750] 2.age:21